KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sg_colors.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-2017 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#include <iostream>
26#include <sstream>
27#include <wx/log.h>
28
31
32
33SGCOLORS::SGCOLORS( SGNODE* aParent ) : SGNODE( aParent )
34{
36
37 if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
38 {
39 m_Parent = nullptr;
40
41 wxLogTrace( MASK_3D_SG,
42 wxT( "%s:%s:%d * [BUG] inappropriate parent to SGCOLORS (type %s)" ),
43 __FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
44 }
45 else if( nullptr != aParent && S3D::SGTYPE_FACESET == aParent->GetNodeType() )
46 {
47 m_Parent->AddChildNode( this );
48 }
49}
50
51
53{
54 colors.clear();
55}
56
57
58bool SGCOLORS::SetParent( SGNODE* aParent, bool notify )
59{
60 if( nullptr != m_Parent )
61 {
62 if( aParent == m_Parent )
63 return true;
64
65 // handle the change in parents
66 if( notify )
68
69 m_Parent = nullptr;
70
71 if( nullptr == aParent )
72 return true;
73 }
74
75 // only a SGFACESET may be parent to a SGCOLORS
76 if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
77 return false;
78
79 m_Parent = aParent;
80
81 if( m_Parent )
82 m_Parent->AddChildNode( this );
83
84 return true;
85}
86
87
88SGNODE* SGCOLORS::FindNode(const char* aNodeName, const SGNODE *aCaller) noexcept
89{
90 if( nullptr == aNodeName || 0 == aNodeName[0] )
91 return nullptr;
92
93 if( !m_Name.compare( aNodeName ) )
94 return this;
95
96 return nullptr;
97}
98
99
100void SGCOLORS::unlinkChildNode( const SGNODE* aCaller ) noexcept
101{
102 wxCHECK( aCaller, /* void */ );
103}
104
105
106void SGCOLORS::unlinkRefNode( const SGNODE* aCaller ) noexcept
107{
108 wxCHECK( aCaller, /* void */ );
109}
110
111
112bool SGCOLORS::AddRefNode( SGNODE* aNode ) noexcept
113{
114 wxCHECK( aNode, false );
115
116 return false;
117}
118
119
120bool SGCOLORS::AddChildNode( SGNODE* aNode ) noexcept
121{
122 wxCHECK( aNode, false );
123
124 return false;
125}
126
127
128bool SGCOLORS::GetColorList( size_t& aListSize, SGCOLOR*& aColorList )
129{
130 if( colors.empty() )
131 {
132 aListSize = 0;
133 aColorList = nullptr;
134 return false;
135 }
136
137 aListSize = colors.size();
138 aColorList = &colors[0];
139 return true;
140}
141
142
143void SGCOLORS::SetColorList( size_t aListSize, const SGCOLOR* aColorList )
144{
145 colors.clear();
146
147 if( 0 == aListSize || nullptr == aColorList )
148 return;
149
150 for( size_t i = 0; i < aListSize; ++i )
151 colors.push_back( aColorList[i] );
152
153 return;
154}
155
156
157void SGCOLORS::AddColor( double aRedValue, double aGreenValue, double aBlueValue )
158{
159 colors.emplace_back( aRedValue, aGreenValue, aBlueValue );
160 return;
161}
162
163
164void SGCOLORS::AddColor( const SGCOLOR& aColor )
165{
166 colors.push_back( aColor );
167 return;
168}
169
170
172{
173 m_written = false;
174
175 // rename this node
176 m_Name.clear();
177 GetName();
178}
179
180
181bool SGCOLORS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
182{
183 if( colors.empty() )
184 return false;
185
186 if( aReuseFlag )
187 {
188 if( !m_written )
189 {
190 aFile << "color DEF " << GetName() << " Color { color [\n ";
191 m_written = true;
192 }
193 else
194 {
195 aFile << "color USE " << GetName() << "\n";
196 return true;
197 }
198 }
199 else
200 {
201 aFile << "color Color { color [\n ";
202 }
203
204 std::string tmp;
205 size_t n = colors.size();
206 bool nline = false;
207
208 for( size_t i = 0; i < n; )
209 {
210 S3D::FormatColor( tmp, colors[i] );
211 float r,g,b;
212 colors[i].GetColor(r, g, b);
213 aFile << tmp ;
214 ++i;
215
216 if( i < n )
217 {
218 aFile << ",";
219
220 if( nline )
221 {
222 aFile << "\n ";
223 nline = false;
224 }
225 else
226 {
227 nline = true;
228 }
229
230 }
231 }
232
233 aFile << "] }\n";
234
235 return true;
236}
237
238
239bool SGCOLORS::WriteCache( std::ostream& aFile, SGNODE* parentNode )
240{
241 if( nullptr == parentNode )
242 {
243 wxCHECK( m_Parent, false );
244
245 SGNODE* np = m_Parent;
246
247 while( nullptr != np->GetParent() )
248 np = np->GetParent();
249
250 if( np->WriteCache( aFile, nullptr ) )
251 {
252 m_written = true;
253 return true;
254 }
255
256 return false;
257 }
258
259 wxCHECK( parentNode == m_Parent, false );
260
261 if( !aFile.good() )
262 {
263 wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] bad stream" ),
264 __FILE__, __FUNCTION__, __LINE__ );
265
266 return false;
267 }
268
269 aFile << "[" << GetName() << "]";
270 size_t ncolors = colors.size();
271 aFile.write( (char*)&ncolors, sizeof(size_t) );
272
273 for( size_t i = 0; i < ncolors; ++i )
274 S3D::WriteColor( aFile, colors[i] );
275
276 if( aFile.fail() )
277 return false;
278
279 m_written = true;
280 return true;
281}
282
283
284bool SGCOLORS::ReadCache( std::istream& aFile, SGNODE* parentNode )
285{
286 wxCHECK( colors.empty(), false );
287
288 size_t ncolors;
289 aFile.read( (char*) &ncolors, sizeof( size_t ) );
290 SGCOLOR tmp;
291
292 if( aFile.fail() )
293 return false;
294
295 for( size_t i = 0; i < ncolors; ++i )
296 {
297 if( !S3D::ReadColor( aFile, tmp ) || aFile.fail() )
298 return false;
299
300 colors.push_back( tmp );
301 }
302
303 return true;
304}
virtual ~SGCOLORS()
Definition: sg_colors.cpp:52
void unlinkChildNode(const SGNODE *aNode) noexcept override
Remove references to an owned child.
Definition: sg_colors.cpp:100
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Write this node's data to a binary cache file.
Definition: sg_colors.cpp:239
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Reads binary format data from a cache file.
Definition: sg_colors.cpp:284
bool AddChildNode(SGNODE *aNode) noexcept override
Definition: sg_colors.cpp:120
bool AddRefNode(SGNODE *aNode) noexcept override
Definition: sg_colors.cpp:112
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Set the parent SGNODE of this object.
Definition: sg_colors.cpp:58
void unlinkRefNode(const SGNODE *aNode) noexcept override
Remove pointers to a referenced node.
Definition: sg_colors.cpp:106
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Writes this node's data to a VRML file.
Definition: sg_colors.cpp:181
SGNODE * FindNode(const char *aNodeName, const SGNODE *aCaller) noexcept override
Search the tree of linked nodes and return a reference to the first node found with the given name.
Definition: sg_colors.cpp:88
void AddColor(double aRedValue, double aGreenValue, double aBlueValue)
Definition: sg_colors.cpp:157
void SetColorList(size_t aListSize, const SGCOLOR *aColorList)
Definition: sg_colors.cpp:143
std::vector< SGCOLOR > colors
Definition: sg_colors.h:64
SGCOLORS(SGNODE *aParent)
Definition: sg_colors.cpp:33
bool GetColorList(size_t &aListSize, SGCOLOR *&aColorList)
Definition: sg_colors.cpp:128
void ReNameNodes(void) override
Rename a node and all its child nodes in preparation for write operations.
Definition: sg_colors.cpp:171
The base class of all Scene Graph nodes.
Definition: sg_node.h:75
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Write this node's data to a binary cache file.
const char * GetName(void)
Definition: sg_node.cpp:146
SGNODE * GetParent(void) const noexcept
Returns a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
Definition: sg_node.cpp:110
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104
virtual bool AddChildNode(SGNODE *aNode)=0
SGNODE * m_Parent
Pointer to parent node; may be NULL for top level transform.
Definition: sg_node.h:227
std::string m_Name
name to use for referencing the entity by name.
Definition: sg_node.h:229
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
bool m_written
Set to true when the object has been written after a ReNameNodes().
Definition: sg_node.h:230
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
bool WriteColor(std::ostream &aFile, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:180
void FormatColor(std::string &result, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:135
@ SGTYPE_COLORS
Definition: sg_types.h:38
@ SGTYPE_FACESET
Definition: sg_types.h:40
bool ReadColor(std::istream &aFile, SGCOLOR &aColor)
Definition: sg_helpers.cpp:295
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...