summaryrefslogtreecommitdiff
path: root/branches/hugues/glagen/3d/dot.cc
blob: 86df62bd4c47c400e375b00752ec089a1072794b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//=============================================================================
//
// Glagen : a planet sized landscape generator
// Copyright (C) 2002  Julien Guertault, Hugues Hiegel, Meng-Tih Lam
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//=============================================================================
//
// Glagen : GPL LAndscape GENerator
//
// dot.cc for Glagen : made by Zavie (Julien Guertault)
//
// www.glagen.org
//
//=============================================================================

#include	<cstdlib>
#include	<cmath>
#include	"data_glagen.hh"
#include	"dot.hh"

// Constructor and destructor
Dot :: Dot (const float x, const float y, const float z):
  _x(x), _y(y), _z(z), _use(0), _computed(false)
{
  _normal = Vector (0, 0, 0);
  _property = new property_t *[glagen.property]; // delete : dot.cc l61
  for (int current = 0; current < glagen.property; current++)
    _property[current] = NULL;
  for (int current = 0; current < 6; current++)
    _owner[current] = NULL;
  step = glagen.step;
}

Dot :: ~Dot ()
{
  if (_property != NULL)
    {
      for (int current = 0; current < glagen.property; current++)
	if (_property[current] != NULL)
	  {
	    if (_property[current]->data != NULL)
	      delete _property[current]->data; // FIXME : warning here.
	    delete _property[current];
	  }
      delete _property;
    }
}


// ======================================================================= Read

float		Dot :: x () const		{ return _x; }
float		Dot :: y () const		{ return _y; }
float		Dot :: z () const		{ return _z; }

Triangle	*Dot :: Owner (int triangle) const
{
  if (triangle >= 0 && triangle < 6)
    return _owner[triangle];
  return NULL;
}

Vector		&Dot :: Normal ()		{ return _normal; }

property_t	*Dot :: Property (const int i) const
{
  return _property[i];
}

bool		Dot :: Is_computed () const
{
  return _computed;
}

bool		Dot :: Is_checked () const
{
  return (step == glagen.step);
}


// ====================================================================== Write

void		Dot :: Set (const float x, const float y, const float z)
{
  _x = x;
  _y = y;
  _z = z;
}

void		Dot :: Use (Triangle *old_one, Triangle *new_one)
{
  Change_owner (old_one, new_one);
  _use = _use + 1;
}

bool		Dot :: Drop (Triangle *old_one, Triangle *new_one)
{
  Change_owner (old_one, new_one);
  _use = _use - 1;
  return (_use <= 0);
}

void		Dot :: Update_normal ()
{
  int		count = 0;

  if (step == glagen.step)
    return;
  step = glagen.step;

  _normal.Set (0, 0, 0);
  for (int current = 0; current < 6; current++)
    if (_owner[current] != NULL)
      {
	_normal = _normal + _owner[current]->Normal_real ();
	count++;
      }
  if (count != 0)
    _normal = _normal / count;
}

void		Dot :: Del_property (const int i)
{
  if (_property[i] != NULL)
    {
      delete _property[i];
      _property[i] = NULL;
    }
}

void		Dot :: Set_property (const int i, property_t *property)
{
  _property[i] = property;
}

void		Dot :: Computed ()		{ _computed = true; }

void		Dot :: Checked ()		{ step = glagen.step; }

// ====================================================================== Other

Dot		*Dot :: Middle (const Dot *b)
{
  float xc = (x () + b->x ()) / 2;
  float yc = (y () + b->y ()) / 2;
  float zc = (z () + b->z ()) / 2;
  float adjust = sqrtf (glagen.square_size / (xc * xc + yc * yc + zc * zc));
  xc = xc * adjust;
  yc = yc * adjust;
  zc = zc * adjust;
  return new Dot (xc, yc, zc); // delete : triangle.cc l99, 101, 103
}

// ================================================================== Protected

void		Dot :: Change_owner (Triangle *old_one, Triangle *new_one)
{
  for (int current = 0; current < 6; current++)
    if (_owner[current] == old_one)
      {
	_owner[current] = new_one;
	return;
      }
  for (int current = 0; current < 6; current++)
    if (_owner[current] == NULL)
      {
	_owner[current] = new_one;
	return;
      }
}