1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2020 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 
25 /**
26  * @file object_3d.h
27  */
28 
29 #ifndef _OBJECT_3D_H_
30 #define _OBJECT_3D_H_
31 
32 #include "bbox_3d.h"
33 #include "../material.h"
34 
35 class BOARD_ITEM;
36 class HITINFOR;
37 
38 enum class OBJECT_3D_TYPE
39 {
40     CYLINDER,
41     DUMMYBLOCK,
42     LAYERITEM,
43     XYPLANE,
44     ROUNDSEG,
45     TRIANGLE,
46     MAX
47 };
48 
49 
50 class OBJECT_3D
51 {
52 public:
53     explicit OBJECT_3D( OBJECT_3D_TYPE aObjType );
54 
SetBoardItem(BOARD_ITEM * aBoardItem)55     const void SetBoardItem( BOARD_ITEM* aBoardItem ) { m_boardItem = aBoardItem; }
GetBoardItem()56     BOARD_ITEM* GetBoardItem() const { return m_boardItem; }
57 
SetMaterial(const MATERIAL * aMaterial)58     void SetMaterial( const MATERIAL* aMaterial )
59     {
60         m_material = aMaterial;
61         m_modelTransparency = aMaterial->GetTransparency(); // Default transparency is from material
62     }
63 
GetMaterial()64     const MATERIAL* GetMaterial() const { return m_material; }
GetModelTransparency()65     float GetModelTransparency() const { return m_modelTransparency; }
SetModelTransparency(float aModelTransparency)66     void SetModelTransparency( float aModelTransparency )
67     {
68         m_modelTransparency = aModelTransparency;
69     }
70 
71     virtual SFVEC3F GetDiffuseColor( const HITINFO& aHitInfo ) const = 0;
72 
~OBJECT_3D()73     virtual ~OBJECT_3D() {}
74 
75     /**
76      * @return true if this object intersects \a aBBox.
77      */
78     virtual bool Intersects( const BBOX_3D& aBBox ) const = 0;
79 
80 
81     /**
82      * @return true if the \a aRay intersects the object.
83      */
84     virtual bool Intersect( const RAY& aRay, HITINFO& aHitInfo ) const = 0;
85 
86     /**
87      * @param aMaxDistance is the maximum distance of the test.
88      * @return true if \a aRay intersects the object.
89      */
90     virtual bool IntersectP( const RAY& aRay, float aMaxDistance ) const = 0;
91 
GetBBox()92     const BBOX_3D& GetBBox() const { return m_bbox; }
93 
GetCentroid()94     const SFVEC3F& GetCentroid() const { return m_centroid; }
95 
96 protected:
97     BBOX_3D m_bbox;
98     SFVEC3F m_centroid;
99     OBJECT_3D_TYPE m_obj_type;
100     const MATERIAL* m_material;
101 
102     BOARD_ITEM* m_boardItem;
103 
104     // m_modelTransparency combines the material and model opacity
105     // 0.0 full opaque, 1.0 full transparent.
106     float m_modelTransparency;
107 };
108 
109 
110 /// Implements a class for object statistics
111 /// using Singleton pattern
112 class OBJECT_3D_STATS
113 {
114 public:
ResetStats()115     void ResetStats()
116     {
117         memset( m_counter, 0, sizeof( unsigned int ) * static_cast<int>( OBJECT_3D_TYPE::MAX ) );
118     }
119 
GetCountOf(OBJECT_3D_TYPE aObjType)120     unsigned int GetCountOf( OBJECT_3D_TYPE aObjType ) const
121     {
122         return m_counter[static_cast<int>( aObjType )];
123     }
124 
AddOne(OBJECT_3D_TYPE aObjType)125     void AddOne( OBJECT_3D_TYPE aObjType )
126     {
127         m_counter[static_cast<int>( aObjType )]++;
128     }
129 
130     // void PrintStats();
131 
Instance()132     static OBJECT_3D_STATS& Instance()
133     {
134         if( !s_instance )
135             s_instance = new OBJECT_3D_STATS;
136 
137         return *s_instance;
138     }
139 
140 private:
OBJECT_3D_STATS()141     OBJECT_3D_STATS(){ ResetStats(); }
142     OBJECT_3D_STATS( const OBJECT_3D_STATS& old );
143     const OBJECT_3D_STATS& operator=( const OBJECT_3D_STATS& old );
~OBJECT_3D_STATS()144     ~OBJECT_3D_STATS() {}
145 
146     unsigned int m_counter[static_cast<int>( OBJECT_3D_TYPE::MAX )];
147 
148     static OBJECT_3D_STATS* s_instance;
149 };
150 
151 
152 #endif // _OBJECT_3D_H_
153