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