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