KiCad PCB EDA Suite
board_adapter.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-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-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 
30 #include "../3d_rendering/ccamera.h"
31 #include "board_adapter.h"
33 #include <class_board.h>
34 #include <3d_math.h>
35 #include "3d_fastmath.h"
37 #include <math/util.h> // for KiROUND
38 #include <pgm_base.h>
40 
47 const wxChar *BOARD_ADAPTER::m_logTrace = wxT( "KI_TRACE_EDA_CINFO3D_VISU" );
48 
49 
51  m_board( nullptr ),
52  m_3d_model_manager( nullptr ),
53  m_colors( nullptr ),
54  m_layerZcoordTop(),
55  m_layerZcoordBottom()
56 {
57  wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::BOARD_ADAPTER" ) );
58 
61  m_drawFlags.resize( FL_LAST, false );
62 
63  if( PgmOrNull() )
64  m_colors = Pgm().GetSettingsManager().GetColorSettings();
65 
68 
69  m_boardPos = wxPoint();
70  m_boardSize = wxSize();
71  m_boardCenter = SFVEC3F( 0.0f );
72 
74 
78 
80  m_epoxyThickness3DU = 0.0f;
81  m_copperThickness3DU = 0.0f;
83  m_biuTo3Dunits = 1.0;
84 
86  m_stats_nr_vias = 0;
88  m_stats_nr_holes = 0;
91 
94 
97  SetFlag( FL_SHOW_BOARD_BODY, true );
102  SetFlag( FL_ZONE, true );
103  SetFlag( FL_SILKSCREEN, true );
104  SetFlag( FL_SOLDERMASK, true );
106 
112 
113  m_BgColorBot = SFVEC4F( 0.4, 0.4, 0.5, 1.0 );
114  m_BgColorTop = SFVEC4F( 0.8, 0.8, 0.9, 1.0 );
115  m_BoardBodyColor = SFVEC4F( 0.4, 0.4, 0.5, 0.9 );
116  m_SolderMaskColorTop = SFVEC4F( 0.1, 0.2, 0.1, 0.83 );
117  m_SolderMaskColorBot = SFVEC4F( 0.1, 0.2, 0.1, 0.83 );
118  m_SolderPasteColor = SFVEC4F( 0.4, 0.4, 0.4, 1.0 );
119  m_SilkScreenColorTop = SFVEC4F( 0.9, 0.9, 0.9, 1.0 );
120  m_SilkScreenColorBot = SFVEC4F( 0.9, 0.9, 0.9, 1.0 );
121  m_CopperColor = SFVEC4F( 0.75, 0.61, 0.23, 1.0 );
122 
125 
126  m_F_Cu_PlatedPads_poly = nullptr;
127  m_B_Cu_PlatedPads_poly = nullptr;
128 
129  // Avoid raytracing options not initialized:
133 
137 
140 }
141 
142 
144 {
145  destroyLayers();
146 }
147 
148 
150 {
151  wxASSERT( aLayer < PCB_LAYER_ID_COUNT );
152 
153  DISPLAY3D_FLG flg;
154 
155  // see if layer needs to be shown
156  // check the flags
157  switch( aLayer )
158  {
159  case B_Adhes:
160  case F_Adhes:
161  flg = FL_ADHESIVE;
162  break;
163 
164  case B_Paste:
165  case F_Paste:
166  flg = FL_SOLDERPASTE;
167  break;
168 
169  case B_SilkS:
170  case F_SilkS:
171  flg = FL_SILKSCREEN;
172  break;
173 
174  case B_Mask:
175  case F_Mask:
176  flg = FL_SOLDERMASK;
177  break;
178 
179  case Dwgs_User:
180  case Cmts_User:
182  return false;
183 
184  flg = FL_COMMENTS;
185  break;
186 
187  case Eco1_User:
188  case Eco2_User:
190  return false;
191 
192  flg = FL_ECO;
193  break;
194 
195  case Edge_Cuts:
197  return false;
198 
199  return true;
200  break;
201 
202  case Margin:
204  return false;
205 
206  return true;
207  break;
208 
209  case B_Cu:
210  case F_Cu:
211  return m_board->IsLayerVisible( aLayer ) || GetFlag( FL_USE_REALISTIC_MODE );
212  break;
213 
214  default:
215  // the layer is an internal copper layer, used the visibility
216  return m_board->IsLayerVisible( aLayer );
217  }
218 
219  // The layer has a flag, return the flag
220  return GetFlag( flg );
221 }
222 
223 
225 {
226  wxASSERT( aFlag < FL_LAST );
227 
228  return m_drawFlags[aFlag];
229 }
230 
231 
232 void BOARD_ADAPTER::SetFlag( DISPLAY3D_FLG aFlag, bool aState )
233 {
234  wxASSERT( aFlag < FL_LAST );
235 
236  m_drawFlags[aFlag] = aState;
237 }
238 
240 {
241  if( aModuleAttributs & MOD_SMD )
243  else if( aModuleAttributs & MOD_THROUGH_HOLE )
245  else
247 }
248 
249 
250 // !TODO: define the actual copper thickness by user
251 #define COPPER_THICKNESS KiROUND( 0.035 * IU_PER_MM ) // for 35 um
252 #define TECH_LAYER_THICKNESS KiROUND( 0.04 * IU_PER_MM )
253 
255 {
256  return COPPER_THICKNESS;
257 }
258 
259 unsigned int BOARD_ADAPTER::GetNrSegmentsCircle( float aDiameter3DU ) const
260 {
261  wxASSERT( aDiameter3DU > 0.0f );
262 
263  return GetNrSegmentsCircle( (int)( aDiameter3DU / m_biuTo3Dunits ) );
264 }
265 
266 
267 unsigned int BOARD_ADAPTER::GetNrSegmentsCircle( int aDiameterBIU ) const
268 {
269  wxASSERT( aDiameterBIU > 0 );
270 
271  return GetArcToSegmentCount( aDiameterBIU / 2, ARC_HIGH_DEF, 360.0 );
272 }
273 
274 
275 void BOARD_ADAPTER::InitSettings( REPORTER* aStatusReporter, REPORTER* aWarningReporter )
276 {
277  wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::InitSettings" ) );
278 
279  // Calculates the board bounding box (board outlines + items)
280  // to ensure any item, even outside the board outlines can be seen
281  EDA_RECT bbbox = m_board->ComputeBoundingBox( false );
282 
283  // Gives a non null size to avoid issues in zoom / scale calculations
284  if( ( bbbox.GetWidth() == 0 ) && ( bbbox.GetHeight() == 0 ) )
285  bbbox.Inflate( Millimeter2iu( 10 ) );
286 
287  m_boardSize = bbbox.GetSize();
288  m_boardPos = bbbox.Centre();
289 
290  wxASSERT( (m_boardSize.x > 0) && (m_boardSize.y > 0) );
291 
292  m_boardPos.y = -m_boardPos.y; // The y coord is inverted in 3D viewer
293 
295 
296  // Ensure the board has 2 sides for 3D views, because it is hard to find
297  // a *really* single side board in the true life...
298  if( m_copperLayersCount < 2 )
300 
301  // Calculate the convertion to apply to all positions.
303 
305 
306  // !TODO: use value defined by user (currently use default values by ctor
309 
310  // Init Z position of each layer
311  // calculate z position for each copper layer
312  // Zstart = -m_epoxyThickness / 2.0 is the z position of the back (bottom layer) (layer id = 31)
313  // Zstart = +m_epoxyThickness / 2.0 is the z position of the front (top layer) (layer id = 0)
314  // all unused copper layer z position are set to 0
315 
316  // ____==__________==________==______ <- Bottom = +m_epoxyThickness / 2.0,
317  // | | Top = Bottom + m_copperThickness
318  // |__________________________________|
319  // == == == == <- Bottom = -m_epoxyThickness / 2.0,
320  // Top = Bottom - m_copperThickness
321 
322  unsigned int layer;
323 
324  for( layer = 0; layer < m_copperLayersCount; ++layer )
325  {
326  m_layerZcoordBottom[layer] = m_epoxyThickness3DU / 2.0f -
327  (m_epoxyThickness3DU * layer / (m_copperLayersCount - 1) );
328 
329  if( layer < (m_copperLayersCount / 2) )
331  else
333  }
334 
335  #define layerThicknessMargin 1.1
336  const float zpos_offset = m_nonCopperLayerThickness3DU * layerThicknessMargin;
337 
338  // Fill remaining unused copper layers and back layer zpos
339  // with -m_epoxyThickness / 2.0
340  for( ; layer < MAX_CU_LAYERS; layer++ )
341  {
342  m_layerZcoordBottom[layer] = -(m_epoxyThickness3DU / 2.0f);
344  }
345 
346  // This is the top of the copper layer thickness.
347  const float zpos_copperTop_back = m_layerZcoordTop[B_Cu];
348  const float zpos_copperTop_front = m_layerZcoordTop[F_Cu];
349 
350  // calculate z position for each non copper layer
351  // Solder mask and Solder paste have the same Z position
352  for( int layer_id = MAX_CU_LAYERS; layer_id < PCB_LAYER_ID_COUNT; ++layer_id )
353  {
354  float zposTop;
355  float zposBottom;
356 
357  switch( layer_id )
358  {
359  case B_Adhes:
360  zposBottom = zpos_copperTop_back - 2.0f * zpos_offset;
361  zposTop = zposBottom - m_nonCopperLayerThickness3DU;
362  break;
363 
364  case F_Adhes:
365  zposBottom = zpos_copperTop_front + 2.0f * zpos_offset;
366  zposTop = zposBottom + m_nonCopperLayerThickness3DU;
367  break;
368 
369  case B_Mask:
370  case B_Paste:
371  zposBottom = zpos_copperTop_back;
372  zposTop = zpos_copperTop_back - m_nonCopperLayerThickness3DU;
373  break;
374 
375  case F_Mask:
376  case F_Paste:
377  zposTop = zpos_copperTop_front + m_nonCopperLayerThickness3DU;
378  zposBottom = zpos_copperTop_front;
379  break;
380 
381  case B_SilkS:
382  zposBottom = zpos_copperTop_back - 1.0f * zpos_offset;
383  zposTop = zposBottom - m_nonCopperLayerThickness3DU;
384  break;
385 
386  case F_SilkS:
387  zposBottom = zpos_copperTop_front + 1.0f * zpos_offset;
388  zposTop = zposBottom + m_nonCopperLayerThickness3DU;
389  break;
390 
391  // !TODO: review
392  default:
393  zposTop = zpos_copperTop_front + (layer_id - MAX_CU_LAYERS + 3.0f) * zpos_offset;
394  zposBottom = zposTop - m_nonCopperLayerThickness3DU;
395  break;
396  }
397 
398  m_layerZcoordTop[layer_id] = zposTop;
399  m_layerZcoordBottom[layer_id] = zposBottom;
400  }
401 
404  0.0f );
405 
406  SFVEC3F boardSize = SFVEC3F( m_boardSize.x * m_biuTo3Dunits,
408  0.0f );
409  boardSize /= 2.0f;
410 
411  SFVEC3F boardMin = (m_boardCenter - boardSize);
412  SFVEC3F boardMax = (m_boardCenter + boardSize);
413 
414  boardMin.z = m_layerZcoordTop[B_Adhes];
415  boardMax.z = m_layerZcoordTop[F_Adhes];
416 
417  m_boardBoundingBox = CBBOX( boardMin, boardMax );
418 
419 #ifdef PRINT_STATISTICS_3D_VIEWER
420  unsigned stats_startCreateBoardPolyTime = GetRunningMicroSecs();
421 #endif
422 
423  if( aStatusReporter )
424  aStatusReporter->Report( _( "Build board body" ) );
425 
426  wxString msg;
427 
428  if( !createBoardPolygon( &msg ) )
429  aWarningReporter->Report( _( "Board outline is not closed: " ) + msg, RPT_SEVERITY_WARNING );
430  else
431  aWarningReporter->Report( wxEmptyString );
432 
433  if( aStatusReporter )
434  aStatusReporter->Report( _( "Create layers" ) );
435 
436  createLayers( aStatusReporter );
437 }
438 
439 
440 bool BOARD_ADAPTER::createBoardPolygon( wxString* aErrorMsg )
441 {
443 
444  wxString errmsg;
445 
446  return m_board->GetBoardPolygonOutlines( m_board_poly, aErrorMsg );
447 }
448 
449 
450 float BOARD_ADAPTER::GetModulesZcoord3DIU( bool aIsFlipped ) const
451 {
452  if( aIsFlipped )
453  {
454  if( GetFlag( FL_SOLDERPASTE ) )
456  else
458  }
459  else
460  {
461  if( GetFlag( FL_SOLDERPASTE ) )
462  return m_layerZcoordTop[F_SilkS];
463  else
464  return m_layerZcoordTop[F_Paste];
465  }
466 }
467 
468 
470 {
471  wxASSERT( aLayerId < PCB_LAYER_ID_COUNT );
472 
473  const COLOR4D color = m_colors->GetColor( aLayerId );
474 
475  return SFVEC4F( color.r, color.g, color.b, color.a );
476 }
477 
478 
480 {
481  return GetColor( m_colors->GetColor( aItemId ) );
482 }
483 
484 
486 {
487  return SFVEC4F( aColor.r, aColor.g, aColor.b, aColor.a );
488 }
GRID3D_TYPE m_3D_grid_type
bool GetFlag(DISPLAY3D_FLG aFlag) const
GetFlag - get a configuration status of a flag.
SFVEC4F GetColor(COLOR4D aColor) const
GetColor.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
Defines math related functions.
#define TECH_LAYER_THICKNESS
SFVEC4F m_SilkScreenColorBot
in realistic mode: SilkScreen color ( bot )
int m_raytrace_nrsamples_refractions
CBVHCONTAINER2D * m_platedpads_container2D_B_Cu
float m_nonCopperLayerThickness3DU
Non copper layers thickness.
float m_raytrace_spread_reflections
SHAPE_POLY_SET * m_B_Cu_PlatedPads_poly
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
void SetFlag(DISPLAY3D_FLG aFlag, bool aState)
SetFlag - set the status of a flag.
COLOR_SETTINGS * m_colors
SFVEC4F m_SolderMaskColorTop
in realistic mode: solder mask color ( top )
int color
Definition: DXF_plotter.cpp:61
CBVHCONTAINER2D m_through_holes_inner
It contains the list of throughHoles of the board, the radius is the inner hole.
int GetWidth() const
Definition: eda_rect.h:119
void InitSettings(REPORTER *aStatusReporter, REPORTER *aWarningReporter)
InitSettings - Function to be called by the render when it need to reload the settings for the board.
SFVEC4F m_BoardBodyColor
in realistic mode: FR4 board color
SFVEC4F m_SolderPasteColor
in realistic mode: solder paste color
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:537
double g
Green component.
Definition: color4d.h:367
unsigned int m_stats_nr_holes
number of holes in the board
#define layerThicknessMargin
SFVEC4F m_SolderMaskColorBot
in realistic mode: solder mask color ( bot )
CBVHCONTAINER2D m_through_holes_outer_ring
It contains the list of throughHoles of the board, the radius of the hole is inflated with the annula...
wxPoint m_boardPos
center board actual position in board units
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
std::array< float, PCB_LAYER_ID_COUNT > m_layerZcoordBottom
Bottom (Start) Z position of each layer (normalized)
float m_epoxyThickness3DU
Epoxy thickness (normalized)
unsigned int m_stats_nr_vias
Nr of vias.
float m_copperThickness3DU
Copper thickness (normalized)
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
double b
Blue component.
Definition: color4d.h:368
RENDER_ENGINE m_render_engine
SHAPE_POLY_SET * m_F_Cu_PlatedPads_poly
PCB_LAYER_ID
A quick note on layer IDs:
double a
Alpha component.
Definition: color4d.h:369
unsigned int GetNrSegmentsCircle(float aDiameter3DU) const
GetNrSegmentsCircle.
float m_raytrace_spread_shadows
SFVEC4F m_BgColorTop
background top color
std::vector< bool > m_drawFlags
std::array< float, PCB_LAYER_ID_COUNT > m_layerZcoordTop
Top (End) Z position of each layer (normalized)
int m_raytrace_nrsamples_shadows
CBBOX m_boardBoundingBox
3d bounding box of the pcb board in 3d units
SHAPE_POLY_SET m_board_poly
PCB board outline polygon.
int GetCopperThicknessBIU() const noexcept
GetCopperThicknessBIU - Get the current copper layer thickness.
Use all material properties from model file.
float m_raytrace_spread_refractions
float m_stats_track_med_width
Track average width.
float m_calc_seg_max_factor3DU
max factor used for cicle segment approximation calculation
double m_biuTo3Dunits
Normalization scale to convert board internal units to 3D units to normalize 3D units between -1....
SFVEC4F m_BgColorBot
background bottom color
bool Is3DLayerEnabled(PCB_LAYER_ID aLayer) const
Is3DLayerEnabled - Check if a layer is enabled.
a few functions useful in geometry calculations.
MODULE_ATTR_T
Enum MODULE_ATTR_T is the set of attributes allowed within a MODULE, using MODULE::SetAttributes() an...
Definition: class_module.h:66
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, wxString *aErrorText=nullptr, wxPoint *aErrorLocation=nullptr)
Function GetBoardPolygonOutlines Extracts the board outlines and build a closed polygon from lines,...
int m_raytrace_recursivelevel_reflections
SFVEC4F GetItemColor(int aItemId) const
GetItemColor - get the technical color of a layer.
void createLayers(REPORTER *aStatusReporter)
int GetHeight() const
Definition: eda_rect.h:120
DISPLAY3D_FLG
Flags used in rendering options.
Definition: 3d_enums.h:34
CBVHCONTAINER2D m_through_holes_outer
It contains the list of throughHoles of the board, the radius of the hole is inflated with the copper...
COLOR4D GetColor(int aLayer) const
wxSize m_boardSize
board actual size in board units
bool IsLayerVisible(PCB_LAYER_ID aLayer) const
Function IsLayerVisible is a proxy function that calls the correspondent function in m_BoardSettings ...
SFVEC4F GetLayerColor(PCB_LAYER_ID aLayerId) const
GetLayerColor - get the technical color of a layer.
see class PGM_BASE
bool ShouldModuleBeDisplayed(MODULE_ATTR_T aModuleAttributs) const
ShouldModuleBeDisplayed - Test if module should be displayed in relation to attributs and the flags.
#define _(s)
Definition: 3d_actions.cpp:33
unsigned GetRunningMicroSecs()
Function GetRunningMicroSecs An alternate way to calculate an elapset time (in microsecondes) to clas...
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
unsigned int m_copperLayersCount
Number of copper layers actually used by the board.
bool createBoardPolygon(wxString *aErrorMsg)
Create the board outline polygon.
SFVEC4F m_CopperColor
in realistic mode: copper color
int GetCopperLayerCount() const
Function GetCopperLayerCount.
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
float GetModulesZcoord3DIU(bool aIsFlipped) const
GetModulesZcoord3DIU - Get the position of the module in 3d integer units considering if it is flippe...
wxPoint Centre() const
Definition: eda_rect.h:62
float m_stats_via_med_hole_diameter
Computed medium diameter of the via holes in 3D units.
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
Defines math related functions.
SFVEC4F m_SilkScreenColorTop
in realistic mode: SilkScreen color ( top )
float m_stats_hole_med_diameter
Computed medium diameter of the holes in 3D units.
SFVEC3F m_boardCenter
3d center position of the pcb board in 3d units
#define COPPER_THICKNESS
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
double r
Red component.
Definition: color4d.h:366
MATERIAL_MODE m_material_mode
PGM_BASE * PgmOrNull()
similat to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
int m_raytrace_recursivelevel_refractions
ANTIALIASING_MODE m_antialiasing_mode
CBVHCONTAINER2D * m_platedpads_container2D_F_Cu
static constexpr int Millimeter2iu(double mm)
float m_calc_seg_min_factor3DU
min factor used for cicle segment approximation calculation
#define RANGE_SCALE_3D
This defines the range that all coord will have to be rendered.
Definition: board_adapter.h:61
CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox.cpp:98
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
unsigned int m_stats_nr_tracks
Number of tracks in the board.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99
int m_raytrace_nrsamples_reflections