Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TEveTriangleSet.cxx
Go to the documentation of this file.
1// @(#)root/eve:$Id$
2// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TEveTriangleSet.h"
13#include "TEveRGBAPalette.h"
14#include "TEveManager.h"
15
16#include "TMath.h"
17#include "TVector3.h"
18#include "TRandom3.h"
19
20
21/** \class TEveTriangleSet
22\ingroup TEve
23Made from a list of vertices and a list of triangles (triplets of
24vertex indices).
25
26If input is composed from triangles with direct vertex coordinates
27one should consider finding all occurrences of the same vertex
28and specifying it only once.
29*/
30
31
32////////////////////////////////////////////////////////////////////////////////
33/// Constructor.
34
36 TEveElementList("TEveTriangleSet", "", kTRUE),
37 fNVerts (nv), fVerts(nullptr),
38 fNTrings (nt), fTrings(nullptr), fTringNorms(nullptr), fTringCols(nullptr)
39{
41
42 fVerts = new Float_t[3*fNVerts];
43 fTrings = new Int_t [3*fNTrings];
44 fTringNorms = (norms) ? new Float_t[3*fNTrings] : nullptr;
45 fTringCols = (cols) ? new UChar_t[3*fNTrings] : nullptr;
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Destructor.
50
52{
53 delete [] fVerts;
54 delete [] fTrings;
55 delete [] fTringNorms;
56 delete [] fTringCols;
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Generate triangle normals via cross product of triangle edges.
61
63{
64 if (fTringNorms == nullptr) fTringNorms = new Float_t[3*fNTrings];
65
66 TVector3 e1, e2, n;
69 for(Int_t t=0; t<fNTrings; ++t, norm+=3, tring+=3)
70 {
71 Float_t* v0 = Vertex(tring[0]);
72 Float_t* v1 = Vertex(tring[1]);
73 Float_t* v2 = Vertex(tring[2]);
74 e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
75 e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
76 n = e1.Cross(e2);
77 n.SetMag(1);
78 n.GetXYZ(norm);
79 }
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Assign random colors to all triangles.
84
86{
87 if (fTringCols == nullptr) fTringCols = new UChar_t[3*fNTrings];
88
89 TRandom r;
90 r.SetSeed(0);
91 UChar_t *col = fTringCols;
92 for(Int_t t=0; t<fNTrings; ++t, col+=3)
93 {
94 col[0] = (UChar_t) r.Uniform(60, 255);
95 col[1] = (UChar_t) r.Uniform(60, 255);
96 col[2] = (UChar_t) r.Uniform(60, 255);
97 }
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Generate triangle colors by the z-component of the normal.
102/// Current palette is taken from gStyle.
103
105 Bool_t interp, Bool_t wrap)
106{
107 if (fTringCols == nullptr) fTringCols = new UChar_t[3*fNTrings];
108 if (fTringNorms == nullptr) GenerateTriangleNormals();
109
110 TEveRGBAPalette pal(min, max, interp, wrap);
111 UChar_t *col = fTringCols;
113 for(Int_t t=0; t<fNTrings; ++t, col+=3, norm+=3)
114 {
115 Int_t v = TMath::Nint(fac * norm[2]);
116 pal.ColorFromValue(v, col, kFALSE);
117 }
118 gEve->Redraw3D();
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Compute bounding box.
123/// Virtual from TAttBBox.
124
126{
127 if (fNVerts <= 0) {
128 BBoxZero();
129 return;
130 }
131
132 BBoxInit();
133 Float_t* v = fVerts;
134 for (Int_t i=0; i<fNVerts; ++i, v += 3)
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Paint this object. Only direct rendering is supported.
140
145
146////////////////////////////////////////////////////////////////////////////////
147/// Read a simple ascii input file describing vertices and triangles.
148
150{
151 static const TEveException kEH("TEveTriangleSet::ReadTrivialFile ");
152
153 FILE* f = fopen(file, "r");
154 if (f == nullptr) {
155 ::Error(kEH, "file '%s' not found.", file);
156 return nullptr;
157 }
158
159 Int_t nv, nt;
160 if (fscanf(f, "%d %d", &nv, &nt) != 2) {
161 fclose(f);
162 throw kEH + "Reading nv, nt failed.";
163 }
164
165 if (nv < 0 || nt < 0) {
166 fclose(f);
167 throw kEH + "Negative number of vertices / triangles specified.";
168 }
169
171
172 Float_t *vtx = ts->Vertex(0);
173 for (Int_t i=0; i<nv; ++i, vtx+=3) {
174 if (fscanf(f, "%f %f %f", &vtx[0], &vtx[1], &vtx[2]) != 3) {
175 fclose(f);
176 throw kEH + TString::Format("Reading vertex data %d failed.", i);
177 }
178 }
179
180 Int_t *tngl = ts->Triangle(0);
181 for (Int_t i=0; i<nt; ++i, tngl+=3) {
182 if (fscanf(f, "%d %d %d", &tngl[0], &tngl[1], &tngl[2]) != 3) {
183 fclose(f);
184 throw kEH + TString::Format("Reading triangle data %d failed.", i);
185 }
186 }
187
188 fclose(f);
189
190 return ts;
191}
#define f(i)
Definition RSha256.hxx:104
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEveManager * gEve
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
void BBoxCheckPoint(Float_t x, Float_t y, Float_t z)
Definition TAttBBox.h:69
void BBoxZero(Float_t epsilon=0, Float_t x=0, Float_t y=0, Float_t z=0)
Create cube of volume (2*epsilon)^3 at (x,y,z).
Definition TAttBBox.cxx:41
void BBoxInit(Float_t infinity=1e6)
Dynamic Float_t[6] X(min,max), Y(min,max), Z(min,max)
Definition TAttBBox.cxx:28
A list of TEveElements.
virtual void InitMainTrans(Bool_t can_edit=kTRUE)
Initialize the main transformation to identity matrix.
virtual void PaintStandard(TObject *id)
Paint object – a generic implementation for EVE elements.
Exception class thrown by TEve classes and macros.
Definition TEveUtil.h:102
void Redraw3D(Bool_t resetCameras=kFALSE, Bool_t dropLogicals=kFALSE)
A generic, speed-optimised mapping from value to RGBA color supporting different wrapping and range t...
Made from a list of vertices and a list of triangles (triplets of vertex indices).
Float_t * fTringNorms
void ComputeBBox() override
Compute bounding box.
void GenerateZNormalColors(Float_t fac=20, Int_t min=-20, Int_t max=20, Bool_t interp=kFALSE, Bool_t wrap=kFALSE)
Generate triangle colors by the z-component of the normal.
void GenerateRandomColors()
Assign random colors to all triangles.
Float_t * Vertex(Int_t i)
~TEveTriangleSet() override
Destructor.
static TEveTriangleSet * ReadTrivialFile(const char *file)
Read a simple ascii input file describing vertices and triangles.
TEveTriangleSet(const TEveTriangleSet &)
void GenerateTriangleNormals()
Generate triangle normals via cross product of triangle edges.
void Paint(Option_t *option="") override
Paint this object. Only direct rendering is supported.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
const Int_t n
Definition legend1.C:16
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition TMath.h:704