]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/TriangleSet.cxx
Missing initialization; fiddle with the track marker-style a bit more.
[u/mrichter/AliRoot.git] / EVE / Reve / TriangleSet.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18
19 #include "TriangleSet.h"
20 #include "RGBAPalette.h"
21
22 #include <TMath.h>
23 #include <TVector3.h>
24 #include <TRandom3.h>
25 #include <TVirtualPad.h>
26 #include <TVirtualViewer3D.h>
27 #include <TBuffer3D.h>
28 #include <TBuffer3DTypes.h>
29
30 using namespace Reve;
31
32 //______________________________________________________________________
33 // TriangleSet
34 //
35 // Made from a list of vertices and a list of triangles (triplets of
36 // vertex indices).
37 //
38 // If input is composed from triangles with direct vertex coordinates
39 // one should consider finding all occurences of the same vertex
40 // and specifying it only once.
41
42 ClassImp(TriangleSet)
43
44 TriangleSet::TriangleSet(Int_t nv, Int_t nt, Bool_t norms, Bool_t cols) :
45   RenderElement(fColor),
46   TNamed("TriangleSet", 0),
47   fNVerts  (nv), fVerts(0),
48   fNTrings (nt), fTrings(0), fTringNorms(0), fTringCols(0),
49   fColor   (2),
50   fHMTrans ()
51 {
52   fVerts  = new Float_t[3*fNVerts];
53   fTrings = new Int_t  [3*fNTrings];
54   fTringNorms = (norms) ? new Float_t[3*fNTrings] : 0;
55   fTringCols  = (cols)  ? new UChar_t[3*fNTrings] : 0;
56 }
57
58 TriangleSet::~TriangleSet()
59 {
60   delete [] fVerts;
61   delete [] fTrings;
62   delete [] fTringNorms;
63   delete [] fTringCols;
64 }
65
66 /**************************************************************************/
67
68 void TriangleSet::GenerateTriangleNormals()
69 {
70   if (fTringNorms == 0)  fTringNorms = new Float_t[3*fNTrings];
71
72   TVector3 e1, e2, n;
73   Float_t *N = fTringNorms;
74   Int_t   *T = fTrings;
75   for(Int_t t=0; t<fNTrings; ++t, N+=3, T+=3)
76     {
77       Float_t* v0 = Vertex(T[0]);
78       Float_t* v1 = Vertex(T[1]);
79       Float_t* v2 = Vertex(T[2]);
80       e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
81       e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
82       n = e1.Cross(e2);
83       n.SetMag(1);
84       n.GetXYZ(N);
85     }
86 }
87
88 void TriangleSet::GenerateRandomColors()
89 {
90   if (fTringCols == 0)  fTringCols = new UChar_t[3*fNTrings];
91
92   TRandom r;
93   r.SetSeed(0);
94   UChar_t *C = fTringCols;
95   for(Int_t t=0; t<fNTrings; ++t, C+=3)
96     {
97       C[0] = (UChar_t) r.Uniform(60, 255);
98       C[1] = (UChar_t) r.Uniform(60, 255);
99       C[2] = (UChar_t) r.Uniform(60, 255);
100     }
101 }
102
103 void TriangleSet::GenerateZNormalColors(Float_t fac, Int_t min, Int_t max,
104                                         Bool_t interp, Bool_t wrap)
105 {
106   if (fTringCols  == 0)  fTringCols = new UChar_t[3*fNTrings];
107   if (fTringNorms == 0)  GenerateTriangleNormals();
108
109   RGBAPalette pal(min, max, interp, wrap);
110   UChar_t *C = fTringCols;
111   Float_t *N = fTringNorms;
112   for(Int_t t=0; t<fNTrings; ++t, C+=3, N+=3)
113     {
114       Int_t v = TMath::Nint(fac * N[2]);
115       pal.ColorFromValue(v, C, kFALSE);
116     }
117   gPad->Modified(); gPad->Update();
118 }
119
120 /**************************************************************************/
121
122 void TriangleSet::ComputeBBox()
123 {
124   if (fNVerts <= 0) {
125     BBoxZero();
126     return;
127   }
128
129   BBoxInit();
130   Float_t* v = fVerts;
131   for (Int_t i=0; i<fNVerts; ++i, v += 3)
132     BBoxCheckPoint(v);
133 }
134
135 void TriangleSet::Paint(Option_t* )
136 {
137   TBuffer3D buffer(TBuffer3DTypes::kGeneric);
138
139   // Section kCore
140   buffer.fID           = this;
141   buffer.fColor        = fColor;
142   buffer.fTransparency = 0;
143   fHMTrans.SetBuffer3D(buffer);
144   buffer.SetSectionsValid(TBuffer3D::kCore);
145    
146   // We fill kCore on first pass and try with viewer
147   Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer);
148   if (reqSections == TBuffer3D::kNone) {
149     return;
150   }
151
152   Error("TriangleSet::Paint", "only direct OpenGL rendering supported.");
153 }
154
155 /**************************************************************************/
156
157 #include <stdio.h>
158
159 TriangleSet* TriangleSet::ReadTrivialFile(const char* file)
160 {
161   FILE* f = fopen(file, "r");
162   if (f == 0) {
163     ::Error("TriangleSet::ReadTrivialFile", Form("file '%s' not found.", file));
164     return 0;
165   }
166
167   Int_t nv, nt;
168   fscanf(f, "%d %d", &nv, &nt);
169
170   TriangleSet* ts = new TriangleSet(nv, nt);
171
172   Float_t *V = ts->Vertex(0);
173   for (Int_t i=0; i<nv; ++i) {
174     fscanf(f, "%f %f %f", V++, V++, V++);
175   }
176
177   Int_t *T = ts->Triangle(0);
178   for (Int_t i=0; i<nt; ++i) {
179     fscanf(f, "%d %d %d", T++, T++, T++);
180   }
181
182   fclose(f);
183
184   return ts;
185 }