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