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-2016 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 /**
27  * @file triangle_3d.h
28  * @brief Implement a triangle ray intersection based on article
29  * http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_7_Kd-Trees_and_More_Speed.shtml
30  * by Jacco Bikker, that implement optimizations based on Ingo Wald's thesis.
31  */
32 
33 
34 #ifndef _TRIANGLE_H_
35 #define _TRIANGLE_H_
36 
37 #include "object_3d.h"
38 
39 /**
40  * A triangle object.
41  */
42 class TRIANGLE : public OBJECT_3D
43 {
44 public:
45     TRIANGLE( const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3 );
46 
47     TRIANGLE( const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3,
48               const SFVEC3F& aFaceNormal );
49 
50     TRIANGLE( const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3,
51               const SFVEC3F& aN1, const SFVEC3F& aN2, const SFVEC3F& aN3 );
52 
53     void SetColor( const SFVEC3F& aColor );
54 
55     void SetColor( const SFVEC3F& aVC0, const SFVEC3F& aVC1, const SFVEC3F& aVC2 );
56 
57     void SetColor( unsigned int aFaceColorRGBA );
58 
59     void SetColor( unsigned int aVertex1ColorRGBA, unsigned int aVertex2ColorRGBA,
60                    unsigned int aVertex3ColorRGBA );
61 
62     void SetUV( const SFVEC2F& aUV1, const SFVEC2F& aUV2, const SFVEC2F& aUV3 );
63 
64     bool Intersect( const RAY& aRay, HITINFO& aHitInfo ) const override;
65     bool IntersectP(const RAY& aRay, float aMaxDistance ) const override;
66     bool Intersects( const BBOX_3D& aBBox ) const override;
67     SFVEC3F GetDiffuseColor( const HITINFO& aHitInfo ) const override;
68 
69 private:
70     void pre_calc_const();
71 
72     SFVEC3F m_normal[3];                // 36
73     SFVEC3F m_vertex[3];                // 36
74     SFVEC3F m_n;                        // 12
75     SFVEC2F m_uv[3];                    // 24
76     unsigned int m_vertexColorRGBA[3];  // 12
77     float m_nu, m_nv, m_nd;             // 12
78     unsigned int m_k;                   // 4
79     float m_bnu, m_bnv;                 // 8
80     float m_cnu, m_cnv;                 // 8
81                                         // 152 bytes (max 160 == 5 * 32)
82 };
83 
84 #endif // _TRIANGLE_H_
85