KiCad PCB EDA Suite
sg_base.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 Cirilo Bernardo <cirilo.bernardo@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #include <cmath>
26 #include <iostream>
27 #include <sstream>
28 #include <wx/log.h>
29 
30 #include "plugins/3dapi/sg_base.h"
31 
32 
34 {
35  red = 0.0;
36  green = 0.0;
37  blue = 0.0;
38 
39  return;
40 }
41 
42 SGCOLOR::SGCOLOR( float aRVal, float aGVal, float aBVal )
43 {
44  if( !checkRange( aRVal, aGVal, aBVal ) )
45  {
46 #ifdef DEBUG
47  std::ostringstream ostr;
48  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
49  ostr << " * [BUG] invalid value passed to constructor";
50  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
51 #endif
52  red = 0.0;
53  green = 0.0;
54  blue = 0.0;
55  return;
56  }
57 
58  red = aRVal;
59  green = aGVal;
60  blue = aBVal;
61  return;
62 }
63 
64 
65 void SGCOLOR::GetColor( float& aRedVal, float& aGreenVal, float& aBlueVal ) const
66 {
67  aRedVal = red;
68  aGreenVal = green;
69  aBlueVal = blue;
70  return;
71 }
72 
73 
74 void SGCOLOR::GetColor( SGCOLOR& aColor ) const
75 {
76  aColor.red = red;
77  aColor.green = green;
78  aColor.blue = blue;
79  return;
80 }
81 
82 
83 void SGCOLOR::GetColor( SGCOLOR* aColor ) const
84 {
85  if( NULL == aColor )
86  {
87  #ifdef DEBUG
88  std::ostringstream ostr;
89  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
90  ostr << " * [BUG] NULL pointer passed for aColor";
91  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
92  #endif
93 
94  return;
95  }
96 
97  aColor->red = red;
98  aColor->green = green;
99  aColor->blue = blue;
100  return;
101 }
102 
103 
104 bool SGCOLOR::SetColor( float aRedVal, float aGreenVal, float aBlueVal )
105 {
106  if( !checkRange( aRedVal, aGreenVal, aBlueVal ) )
107  return false;
108 
109  red = aRedVal;
110  green = aGreenVal;
111  blue = aBlueVal;
112 
113  return true;
114 }
115 
116 
117 bool SGCOLOR::SetColor( const SGCOLOR& aColor )
118 {
119  red = aColor.red;
120  green = aColor.green;
121  blue = aColor.blue;
122  return true;
123 }
124 
125 
126 bool SGCOLOR::SetColor( const SGCOLOR* aColor )
127 {
128  if( NULL == aColor )
129  {
130  #ifdef DEBUG
131  std::ostringstream ostr;
132  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
133  ostr << " * [BUG] NULL pointer passed for aColor";
134  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
135  #endif
136 
137  return false;
138  }
139 
140  red = aColor->red;
141  green = aColor->green;
142  blue = aColor->blue;
143  return true;
144 }
145 
146 
147 bool SGCOLOR::checkRange( float aRedVal, float aGreenVal, float aBlueVal ) const
148 {
149  bool ok = true;
150 
151  if( aRedVal < 0.0 || aRedVal > 1.0 )
152  {
153 #ifdef DEBUG
154  std::ostringstream ostr;
155  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
156  ostr << " * [BUG] invalid RED value: " << aRedVal;
157  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
158 #endif
159  ok = false;
160  }
161 
162  if( aGreenVal < 0.0 || aGreenVal > 1.0 )
163  {
164 #ifdef DEBUG
165  if( ok )
166  {
167  wxLogTrace( MASK_3D_SG, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__ );
168  }
169  wxLogTrace( MASK_3D_SG, " * [BUG] invalid GREEN value: %f\n", aGreenVal );
170 #endif
171  ok = false;
172  }
173 
174  if( aBlueVal < 0.0 || aBlueVal > 1.0 )
175  {
176 #ifdef DEBUG
177  if( ok )
178  {
179  wxLogTrace( MASK_3D_SG, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__ );
180  }
181  wxLogTrace( MASK_3D_SG, " * [BUG] invalid BLUE value: %f\n", aBlueVal );
182 #endif
183  ok = false;
184  }
185 
186  return ok;
187 }
188 
189 
191 {
192  x = 0.0;
193  y = 0.0;
194  z = 0.0;
195  return;
196 }
197 
198 
199 SGPOINT::SGPOINT( double aXVal, double aYVal, double aZVal )
200 {
201  x = aXVal;
202  y = aYVal;
203  z = aZVal;
204 }
205 
206 
207 void SGPOINT::GetPoint( double& aXVal, double& aYVal, double& aZVal )
208 {
209  x = aXVal;
210  y = aYVal;
211  z = aZVal;
212  return;
213 }
214 
215 
216 void SGPOINT::GetPoint( SGPOINT& aPoint )
217 {
218  x = aPoint.x;
219  y = aPoint.y;
220  z = aPoint.z;
221  return;
222 }
223 
224 
225 void SGPOINT::GetPoint( SGPOINT* aPoint )
226 {
227  if( NULL == aPoint )
228  {
229  #ifdef DEBUG
230  std::ostringstream ostr;
231  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
232  ostr << " * [BUG] NULL pointer passed for aPoint";
233  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
234  #endif
235 
236  return;
237  }
238 
239  x = aPoint->x;
240  y = aPoint->y;
241  z = aPoint->z;
242  return;
243 }
244 
245 
246 void SGPOINT::SetPoint( double aXVal, double aYVal, double aZVal )
247 {
248  x = aXVal;
249  y = aYVal;
250  z = aZVal;
251  return;
252 }
253 
254 
255 void SGPOINT::SetPoint( const SGPOINT& aPoint )
256 {
257  x = aPoint.x;
258  y = aPoint.y;
259  z = aPoint.z;
260  return;
261 }
262 
263 
265 {
266  vx = 0.0;
267  vy = 0.0;
268  vz = 1.0;
269  return;
270 }
271 
272 
273 SGVECTOR::SGVECTOR( double aXVal, double aYVal, double aZVal )
274 {
275  vx = aXVal;
276  vy = aYVal;
277  vz = aZVal;
278  normalize();
279  return;
280 }
281 
282 
283 void SGVECTOR::GetVector( double& aXVal, double& aYVal, double& aZVal ) const
284 {
285  aXVal = vx;
286  aYVal = vy;
287  aZVal = vz;
288  return;
289 }
290 
291 
292 void SGVECTOR::SetVector( double aXVal, double aYVal, double aZVal )
293 {
294  vx = aXVal;
295  vy = aYVal;
296  vz = aZVal;
297  normalize();
298  return;
299 }
300 
301 
302 void SGVECTOR::SetVector( const SGVECTOR& aVector )
303 {
304  aVector.GetVector( vx, vy, vz );
305  return;
306 }
307 
308 
310 {
311  double dx = vx * vx;
312  double dy = vy * vy;
313  double dz = vz * vz;
314  double dv2 = sqrt( dx + dy + dz );
315 
316  if( (dx + dy + dz) < 1e-8 )
317  {
318  // use the default; the numbers are too small
319  // to be believable
320  vx = 0.0;
321  vy = 0.0;
322  vz = 1.0;
323  return;
324  }
325 
326  vx /= dv2;
327  vy /= dv2;
328  vz /= dv2;
329 
330  return;
331 }
332 
333 
335 {
336  vx = source.vx;
337  vy = source.vy;
338  vz = source.vz;
339  return *this;
340 }
float blue
Definition: sg_base.h:48
double x
Definition: sg_base.h:70
float red
Definition: sg_base.h:46
void SetVector(double aXVal, double aYVal, double aZVal)
Definition: sg_base.cpp:292
void GetPoint(double &aXVal, double &aYVal, double &aZVal)
Definition: sg_base.cpp:207
bool SetColor(float aRedVal, float aGreenVal, float aBlueVal)
Definition: sg_base.cpp:104
double y
Definition: sg_base.h:71
SGCOLOR()
Definition: sg_base.cpp:33
double vz
Definition: sg_base.h:94
void normalize(void)
Definition: sg_base.cpp:309
SGPOINT()
Definition: sg_base.cpp:190
SGVECTOR & operator=(const SGVECTOR &source)
Definition: sg_base.cpp:334
defines the low level classes common to scene graph nodes
double vx
Definition: sg_base.h:92
double z
Definition: sg_base.h:72
double vy
Definition: sg_base.h:93
void GetVector(double &aXVal, double &aYVal, double &aZVal) const
Definition: sg_base.cpp:283
float green
Definition: sg_base.h:47
bool checkRange(float aRedVal, float aGreenVal, float aBlueVal) const
Definition: sg_base.cpp:147
void SetPoint(double aXVal, double aYVal, double aZVal)
Definition: sg_base.cpp:246
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const
Definition: sg_base.cpp:65
SGVECTOR()
Definition: sg_base.cpp:264