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