1
unit LUX.Brep.Face.TriFlip.D3;
3
interface //#################################################################### ■
5
uses System.RegularExpressions,
6
LUX, LUX.D3, LUX.Geometry.D3, LUX.Brep.Face.TriFlip;
8
type //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【型】
13
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【レコード】
15
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【クラス】
17
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriPoin3D
19
TTriPoin3D = class( TTriPoin<TSingle3D> )
26
property Nor :TSingle3D read _Nor write _Nor;
27
property Tex :TSingle3D read _Tex write _Tex;
32
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriFace3D
34
TTriFace3D = class( TTriFace<TSingle3D> )
38
function GetEdge( const I_:Byte ) :TSingle3D;
39
function GetCircumSphere :TSingleSphere;
42
property Edge[ const I_:Byte ] :TSingle3D read GetEdge ;
43
property CircumSphere :TSingleSphere read GetCircumSphere;
46
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriFaceModel3D<_TPoin_,_TFace_>
48
TTriFaceModel3D<_TPoin_:TTriPoin3D;
49
_TFace_:TTriFace3D> = class( TTriFaceModel<TSingle3D,_TPoin_,_TFace_> )
52
function GetVec( const G_:TGroupCollection ) :TSingle3D;
53
function GetPoin( const M_:TMatch; const Ts_,Ns_:TArray<TSingle3D> ) :TTriPoin3D;
54
procedure AddFace( const P1_,P2_,P3_:TTriPoin3D );
59
procedure MakeNormals;
60
procedure LoadFromFile( const FileName_:String );
61
function IsInside( const P_:TSingle3D ) :Single;
62
function GetBoundingBox :TSingleArea3D;
65
TTriFaceModel3D = TTriFaceModel3D<TTriPoin3D,TTriFace3D>;
67
//const //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【定数】
69
//var //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【変数】
71
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【ルーチン】
73
implementation //############################################################### ■
75
uses System.Classes, System.SysUtils;
77
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【レコード】
79
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【クラス】
81
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriPoin3D
83
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& private
85
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected
87
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public
89
/////////////////////////////////////////////////////////////////////// メソッド
91
procedure TTriPoin3D.MakeNormal;
93
N, K, X1, X2, X3, Y1, Y2, Y3 :Integer;
95
V, P1, P2, P3 :TSingle3D;
100
V := TSingle3D.Create( 0, 0, 0 );
102
XF := TTriFace3D( Face );
108
P1 := XF.Poin[ X1 ].Pos;
109
P2 := XF.Poin[ X2 ].Pos;
110
P3 := XF.Poin[ X3 ].Pos;
112
V := V + CrossProduct( P2 - P1, P3 - P2 );
114
YF := TTriFace3D( XF.Face[ X2 ] );
119
XF := YF; X1 := Y1; X2 := Y2; X3 := Y3;
124
if L2 < 0.000001 then _Nor := TSingle3D.Create( 0, 0, 0 )
125
else _Nor := V / Roo2( L2 );
128
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriFace3D
130
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& private
132
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected
134
/////////////////////////////////////////////////////////////////////// アクセス
136
function TTriFace3D.GetEdge( const I_:Byte ) :TSingle3D;
138
Result := Poin[ _Inc_[ I_ ] ].Pos.VectorTo( Poin[ _Dec_[ I_ ] ].Pos );
141
function TTriFace3D.GetCircumSphere :TSingleSphere;
143
Result := TSingleSphere.Create( Poin[ 1 ].Pos, Poin[ 2 ].Pos, Poin[ 3 ].Pos );
146
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public
148
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTriFaceModel3D<_TPoin_,_TFace_>
150
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& private
152
/////////////////////////////////////////////////////////////////////// メソッド
154
function TTriFaceModel3D<_TPoin_,_TFace_>.GetVec( const G_:TGroupCollection ) :TSingle3D;
158
X := G_.Item[ 1 ].Value.ToSingle;
159
Y := G_.Item[ 2 ].Value.ToSingle;
160
Z := G_.Item[ 3 ].Value.ToSingle;
164
function TTriFaceModel3D<_TPoin_,_TFace_>.GetPoin( const M_:TMatch; const Ts_,Ns_:TArray<TSingle3D> ) :TTriPoin3D;
168
if M_.Success and TryStrToInt( M_.Groups[ 1 ].Value, N ) then
170
Result := TTriPoin3D( PoinModel.Childs[ N-1 ] );
172
if TryStrToInt( M_.Groups[ 2 ].Value, N ) then Result._Tex := Ts_[ N-1 ];
173
if TryStrToInt( M_.Groups[ 3 ].Value, N ) then Result._Nor := Ns_[ N-1 ];
178
procedure TTriFaceModel3D<_TPoin_,_TFace_>.AddFace( const P1_,P2_,P3_:TTriPoin3D );
180
with TTriFace3D( TTriFace3D.Create( Self ) ) do
182
Poin[ 1 ] := P1_; BasteCorn( 1 );
183
Poin[ 2 ] := P2_; BasteCorn( 2 );
184
Poin[ 3 ] := P3_; BasteCorn( 3 );
188
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected
190
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public
192
/////////////////////////////////////////////////////////////////////// メソッド
194
procedure TTriFaceModel3D<_TPoin_,_TFace_>.JoinEdges;
200
for I := 0 to ChildsN-1 do Childs[ I ].JoinEdge;
204
procedure TTriFaceModel3D<_TPoin_,_TFace_>.MakeNormals;
210
for I := 0 to ChildsN-1 do TTriPoin3D( Childs[ I ] ).MakeNormal;
214
procedure TTriFaceModel3D<_TPoin_,_TFace_>.LoadFromFile( const FileName_:String );
216
Ns, Ts :TArray<TSingle3D>;
217
RV, RT, RN, RF, RI :TRegEx;
219
Ms :TMatchCollection;
220
P1, P2, P3 :TTriPoin3D;
225
RV := TRegEx.Create( 'v[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t\n]+)' );
226
RT := TRegEx.Create( 'vt[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t\n]+)' );
227
RN := TRegEx.Create( 'vn[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t\n]+)' );
231
with TStreamReader.Create( FileName_, TEncoding.Default ) do
233
while not EndOfStream do
237
with RV.Match( S ) do
239
if Success then TTriPoin3D( TTriPoin3D.Create( PoinModel ) ).Pos := GetVec( Groups );
242
with RT.Match( S ) do
244
if Success then Ts := Ts + [ GetVec( Groups ) ];
247
with RN.Match( S ) do
249
if Success then Ns := Ns + [ GetVec( Groups ) ];
256
RF := TRegEx.Create( 'f( [^\n]+)' );
257
RI := TRegEx.Create( '[ \t]+(\d+)/?(\d*)/?(\d*)' );
259
with TStreamReader.Create( FileName_, TEncoding.Default ) do
261
while not EndOfStream do
263
with RF.Match( ReadLine ) do
267
Ms := RI.Matches( Groups[ 1 ].Value );
269
P1 := GetPoin( Ms[ 0 ], Ts, Ns );
270
P2 := GetPoin( Ms[ 1 ], Ts, Ns );
271
P3 := GetPoin( Ms[ 2 ], Ts, Ns );
273
AddFace( P1, P2, P3 );
275
for I := 3 to Ms.Count-1 do
277
P2 := P3; P3 := GetPoin( Ms[ I ], Ts, Ns );
279
AddFace( P1, P2, P3 );
291
function TTriFaceModel3D<_TPoin_,_TFace_>.IsInside( const P_:TSingle3D ) :Single;
296
for I := 0 to ChildsN-1 do
298
with TTriFace3D( Childs[ I ] ) do
300
Result := Result + SolidAngle( Poin[ 1 ].Pos, Poin[ 2 ].Pos, Poin[ 3 ].Pos, P_ );
304
Result := Result / Pi4;
307
function TTriFaceModel3D<_TPoin_,_TFace_>.GetBoundingBox :TSingleArea3D;
311
Result := TSingleArea3D.NeInf;
315
for I := 0 to ChildsN-1 do
317
with Childs[ I ].Pos do
319
if X < Result.Min.X then Result.Min.X := X;
320
if Y < Result.Min.Y then Result.Min.Y := Y;
321
if Z < Result.Min.Z then Result.Min.Z := Z;
323
if Result.Max.X < X then Result.Max.X := X;
324
if Result.Max.Y < Y then Result.Max.Y := Y;
325
if Result.Max.Z < Z then Result.Max.Z := Z;
331
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【ルーチン】
333
//############################################################################## □
335
initialization //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 初期化
337
finalization //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 最終化
339
end. //######################################################################### ■