]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/TriangleSet.cxx
Added some comments.
[u/mrichter/AliRoot.git] / EVE / Reve / TriangleSet.cxx
1 // $Header$
2
3 #include "TriangleSet.h"
4 #include "RGBAPalette.h"
5
6 #include <TVector3.h>
7 #include <TRandom3.h>
8 #include <TVirtualPad.h>
9 #include <TVirtualViewer3D.h>
10 #include <TBuffer3D.h>
11 #include <TBuffer3DTypes.h>
12
13 using namespace Reve;
14
15 //______________________________________________________________________
16 // TriangleSet
17 //
18 // Made from a list of vertices and a list of triangles (triplets of
19 // vertex indices).
20 //
21 // If input is composed from triangles with direct vertex coordinates
22 // one should consider finding all occurences of the same vertex
23 // and specifying it only once.
24
25 ClassImp(TriangleSet)
26
27 TriangleSet::TriangleSet(Int_t nv, Int_t nt, Bool_t norms, Bool_t cols) :
28   RenderElement(fColor),
29   TNamed("TriangleSet", 0),
30   fNVerts  (nv), fVerts(0),
31   fNTrings (nt), fTrings(0), fTringNorms(0), fTringCols(0),
32   fColor   (2),
33   fHMTrans ()
34 {
35   fVerts  = new Float_t[3*fNVerts];
36   fTrings = new Int_t  [3*fNTrings];
37   fTringNorms = (norms) ? new Float_t[3*fNTrings] : 0;
38   fTringCols  = (cols)  ? new UChar_t[3*fNTrings] : 0;
39 }
40
41 TriangleSet::~TriangleSet()
42 {
43   delete [] fVerts;
44   delete [] fTrings;
45   delete [] fTringNorms;
46   delete [] fTringCols;
47 }
48
49 /**************************************************************************/
50
51 void TriangleSet::GenerateTriangleNormals()
52 {
53   if (fTringNorms == 0)  fTringNorms = new Float_t[3*fNTrings];
54
55   TVector3 e1, e2, n;
56   Float_t *N = fTringNorms;
57   Int_t   *T = fTrings;
58   for(Int_t t=0; t<fNTrings; ++t, N+=3, T+=3)
59     {
60       Float_t* v0 = Vertex(T[0]);
61       Float_t* v1 = Vertex(T[1]);
62       Float_t* v2 = Vertex(T[2]);
63       e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
64       e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
65       n = e1.Cross(e2);
66       n.SetMag(1);
67       n.GetXYZ(N);
68     }
69 }
70
71 void TriangleSet::GenerateRandomColors()
72 {
73   if (fTringCols == 0)  fTringCols = new UChar_t[3*fNTrings];
74
75   TRandom r;
76   r.SetSeed(0);
77   UChar_t *C = fTringCols;
78   for(Int_t t=0; t<fNTrings; ++t, C+=3)
79     {
80       C[0] = (UChar_t) r.Uniform(60, 255);
81       C[1] = (UChar_t) r.Uniform(60, 255);
82       C[2] = (UChar_t) r.Uniform(60, 255);
83     }
84 }
85
86 void TriangleSet::GenerateZNormalColors(Float_t fac, Int_t min, Int_t max,
87                                         Bool_t interp, Bool_t wrap)
88 {
89   if (fTringCols  == 0)  fTringCols = new UChar_t[3*fNTrings];
90   if (fTringNorms == 0)  GenerateTriangleNormals();
91
92   RGBAPalette pal(min, max, interp, wrap);
93   UChar_t *C = fTringCols;
94   Float_t *N = fTringNorms;
95   for(Int_t t=0; t<fNTrings; ++t, C+=3, N+=3)
96     {
97       Int_t v = TMath::Nint(fac * N[2]);
98       pal.ColorFromValue(v, C, kFALSE);
99     }
100   gPad->Modified(); gPad->Update();
101 }
102
103 /**************************************************************************/
104
105 void TriangleSet::ComputeBBox()
106 {
107   if (fNVerts <= 0) {
108     BBoxZero();
109     return;
110   }
111
112   BBoxInit();
113   Float_t* v = fVerts;
114   for (Int_t i=0; i<fNVerts; ++i, v += 3)
115     BBoxCheckPoint(v);
116 }
117
118 void TriangleSet::Paint(Option_t* )
119 {
120   TBuffer3D buffer(TBuffer3DTypes::kGeneric);
121
122   // Section kCore
123   buffer.fID           = this;
124   buffer.fColor        = fColor;
125   buffer.fTransparency = 0;
126   fHMTrans.SetBuffer3D(buffer);
127   buffer.SetSectionsValid(TBuffer3D::kCore);
128    
129   // We fill kCore on first pass and try with viewer
130   Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer);
131   if (reqSections == TBuffer3D::kNone) {
132     return;
133   }
134
135   Error("TriangleSet::Paint", "only direct OpenGL rendering supported.");
136 }
137
138 /**************************************************************************/
139
140 #include <stdio.h>
141
142 TriangleSet* TriangleSet::ReadTrivialFile(const char* file)
143 {
144   FILE* f = fopen(file, "r");
145   if (f == 0) {
146     ::Error("TriangleSet::ReadTrivialFile", Form("file '%s' not found.", file));
147     return 0;
148   }
149
150   Int_t nv, nt;
151   fscanf(f, "%d %d", &nv, &nt);
152
153   TriangleSet* ts = new TriangleSet(nv, nt);
154
155   Float_t *V = ts->Vertex(0);
156   for (Int_t i=0; i<nv; ++i) {
157     fscanf(f, "%f %f %f", V++, V++, V++);
158   }
159
160   Int_t *T = ts->Triangle(0);
161   for (Int_t i=0; i<nt; ++i) {
162     fscanf(f, "%d %d %d", T++, T++, T++);
163   }
164
165   fclose(f);
166
167   return ts;
168 }