Skip to content
Newer
Older
100644 172 lines (158 sloc) 5.27 KB
a552d9c @giniu initial import
giniu authored
1 %%
2 %% wpc_torus.erl --
3 %%
4 %% Torus Plugin plus two other types of tori borrowed from LightFlow.
5 %%
4ade2ce copyright update 2011
Richard Jones authored
6 %% Copyright (c) 2002-2011 Anthony D'Agostino
a552d9c @giniu initial import
giniu authored
7 %%
8 %% See the file "license.terms" for information on usage and redistribution
9 %% of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 %%
868f36e cleanup
Anthony D'Agostino authored
11 %% $Id$
a552d9c @giniu initial import
giniu authored
12 %%
13
14 %% The U resolution is the number of faces that will be generated
15 %% along the path of the major radius; the V resolution, along the path
16 %% of the minor radius.
17 %%
18 %% To generate square faces, rather than long and thin rectangles,
19 %% maintain the following ratio: URes/VRes = MajorRad/MinorRad
20 %% Or you can enter a zero for either U or V and it will automatically adjust.
21
22 -module(wpc_torus).
23 -export([init/0,menu/2,command/2]).
24
374c928 @bjorng Changes by Richard Carlsson to eliminate unnecessary imports and warn…
bjorng authored
25 -import(math, [cos/1,sin/1,pi/0]).
a552d9c @giniu initial import
giniu authored
26 -include("wings_intl.hrl").
27
28 init() -> true.
29
dbb82ce NOTE: New interface features including:
Richard Jones authored
30 menu({shape}, []) ->
a552d9c @giniu initial import
giniu authored
31 torus_menu();
dbb82ce NOTE: New interface features including:
Richard Jones authored
32 menu({shape}, Menu) ->
a552d9c @giniu initial import
giniu authored
33 Menu ++ [separator|torus_menu()];
34 menu(_, Menu) -> Menu.
35
36 torus_menu() ->
37 [{?__(1,"UV Torus") ,uvtorus,[option]},
38 {?__(2,"Lumpy Torus") ,lutorus,[option]},
39 {?__(3,"Spiral Torus") ,sptorus,[option]}].
40
dbb82ce NOTE: New interface features including:
Richard Jones authored
41 command({shape,{uvtorus,Ask}}, _St) -> make_uv_torus(Ask);
42 command({shape,{lutorus,Ask}}, _St) -> make_lu_torus(Ask);
43 command({shape,{sptorus,Ask}}, _St) -> make_sp_torus(Ask);
a552d9c @giniu initial import
giniu authored
44 command(_, _) -> next.
45
46 %%% The rest are local functions.
47
48 % ======= Regular Torus =======
49 make_uv_torus(Ask) when is_atom(Ask) ->
50 wpa:ask(Ask, ?__(1,"UV Torus Options"),
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
51 [{?__(2,"U Resolution"),80,
52 [{range,{0,infinity}},{info,?__(6,"Number of segments along major circumference (U, minimum is 3)")}]},
53 {?__(3,"V Resolution"),16,
54 [{range,{0,infinity}},{info,?__(7,"Number of segments along minor circumference (V, minimum is 3)")}]},
a552d9c @giniu initial import
giniu authored
55 {?__(4,"Major Radius"),1.0},
56 {?__(5,"Minor Radius"),0.2}],
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
57 fun(Res) ->
58 {shape,{uvtorus,Res}}
59 end);
a552d9c @giniu initial import
giniu authored
60 make_uv_torus([URES, VRES, MajR, MinR]) ->
61 case {URES>0, VRES>0} of
62 {true, true} ->
63 Ures = URES,
64 Vres = VRES;
65 {false, true} ->
66 Ures = trunc(VRES*(MajR/MinR)),
67 Vres = VRES;
68 {true, false} ->
69 Vres = trunc(URES/(MajR/MinR)),
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
70 Ures = URES;
71 {false, false} ->
72 Vres = 6,
73 Ures = trunc(Vres*(MajR/MinR))
a552d9c @giniu initial import
giniu authored
74 end,
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
75 Ures0=min_uv_torus_res(Ures),
76 Vres0=min_uv_torus_res(Vres),
77 Vs = make_verts(Ures0, Vres0, MajR, MinR, none, none, 1),
78 Fs = make_faces(Ures0, Vres0),
a552d9c @giniu initial import
giniu authored
79 {new_shape,"UV Torus",Fs,Vs}.
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
80 min_uv_torus_res(Res) when Res =< 3 -> 3;
81 min_uv_torus_res(Res) -> Res.
a552d9c @giniu initial import
giniu authored
82
83 % ======= Lumpy Torus =======
84 make_lu_torus(Ask) when is_atom(Ask) ->
85 wpa:ask(Ask, ?__(1,"Lumpy Torus Options"),
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
86 [{?__(2,"U Resolution"),125,
87 [{range,{0,infinity}},{info,?__(8,"Number of segments along major circumference (U, minimum is 3)")}]},
88 {?__(3,"V Resolution"),25,
89 [{range,{0,infinity}},{info,?__(9,"Number of segments along minor circumference (V, minimum is 3)")}]},
a552d9c @giniu initial import
giniu authored
90 {?__(4,"Major Radius"),1.0},
91 {?__(5,"Minor Radius"),0.2},
92 {?__(6,"Lumps"),8},
93 {?__(7,"Lump Amplitude"),0.5}],
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
94 fun(Res) ->
95 {shape,{lutorus,Res}}
96 end);
97 make_lu_torus([Ures0, Vres0, MajR, MinR, Loops, LoopRad]) ->
98 Ures=min_uv_torus_res(Ures0),
99 Vres=min_uv_torus_res(Vres0),
868f36e cleanup
Anthony D'Agostino authored
100 Vs = make_verts(Ures, Vres, MajR, MinR, Loops, LoopRad, 2),
101 Fs = make_faces(Ures, Vres),
a552d9c @giniu initial import
giniu authored
102 {new_shape,"Lumpy Torus",Fs,Vs}.
103
104 % ======= Spiral Torus =======
105 make_sp_torus(Ask) when is_atom(Ask) ->
106 wpa:ask(Ask, ?__(1,"Spiral Torus Options"),
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
107 [{?__(2,"U Resolution"),200,
108 [{range,{0,infinity}},{info,?__(8,"Number of segments along major circumference (U, minimum is 3)")}]},
109 {?__(3,"V Resolution"),20,
110 [{range,{0,infinity}},{info,?__(9,"Number of segments along minor circumference (V, minimum is 3)")}]},
a552d9c @giniu initial import
giniu authored
111 {?__(4,"Major Radius"),1.0},
112 {?__(5,"Minor Radius"),0.2},
113 {?__(6,"Loops"),8},
114 {?__(7,"Loop Radius "),0.2}],
ba26d88 @Micheus NOTE: Fixed UV Torus crash
Micheus authored
115 fun(Res) ->
116 {shape,{sptorus,Res}}
117 end);
118 make_sp_torus([Ures0, Vres0, MajR, MinR, Loops, LoopRad]) ->
119 Ures=min_uv_torus_res(Ures0),
120 Vres=min_uv_torus_res(Vres0),
868f36e cleanup
Anthony D'Agostino authored
121 Vs = make_verts(Ures, Vres, MajR, MinR, Loops, LoopRad, 3),
122 Fs = make_faces(Ures, Vres),
a552d9c @giniu initial import
giniu authored
123 {new_shape,"Spiral Torus",Fs,Vs}.
124
868f36e cleanup
Anthony D'Agostino authored
125 make_verts(Ures, Vres, MajR, MinR, Loops, LoopRad, Type) ->
126 Us = lists:seq(0, Ures-1),
127 Vs = lists:seq(0, Vres-1),
a552d9c @giniu initial import
giniu authored
128 Du = 2*pi()/Ures,
129 Dv = 2*pi()/Vres,
868f36e cleanup
Anthony D'Agostino authored
130 Make_Vert = case Type of
131 1->
132 fun(I,J) ->
133 {A,B,C,D} = {cos(J*Dv), sin(J*Dv), cos(I*Du), sin(I*Du)},
134 X = (MajR + MinR*A) * C,
135 Y = -(MinR*B),
136 Z = (MajR + MinR*A) * D,
137 {X,Y,Z}
138 end;
139 2 ->
140 fun(I,J) ->
141 {A,B,C,D} = {cos(J*Dv), sin(J*Dv), cos(I*Du), sin(I*Du)},
142 N = 1+cos(I*Du*Loops)*LoopRad,
143 X = (MajR + MinR*A*N) * C,
144 Y = -(MinR*B*N),
145 Z = (MajR + MinR*A*N) * D,
146 {X,Y,Z}
147 end;
148 3 ->
149 fun(I,J) ->
150 {A,B,C,D} = {cos(J*Dv), sin(J*Dv), cos(I*Du), sin(I*Du)},
151 N = sin(I*Du*Loops)*LoopRad,
152 O = cos(I*Du*Loops)*LoopRad,
153 X = (MajR + MinR*A + N) * C,
154 Y = -(MinR*B + O),
155 Z = (MajR + MinR*A + N) * D,
156 {X,Y,Z}
157 end
158 end,
159 [Make_Vert(I,J) || I <- Us, J <- Vs].
160
161 make_faces(Ures, Vres) ->
162 Us = lists:seq(0, Ures-1),
163 Vs = lists:seq(0, Vres-1),
164 Make_Face = fun(I,J) ->
165 Idx1 = (J+1) rem Vres + I*Vres,
166 Idx2 = (J+1) rem Vres + ((I+1) rem Ures)*Vres,
167 Idx3 = J + ((I+1) rem Ures) * Vres,
168 Idx4 = J + I*Vres,
169 [Idx4,Idx3,Idx2,Idx1]
170 end,
171 [Make_Face(I,J) || I <- Us, J <- Vs].
Something went wrong with that request. Please try again.