1 /* PovrayUtils.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9 
10   The above copyright notice and this permission notice shall be included in all copies or substantial portions
11   of the Software.
12 
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19 
20 
21 #include "../../Config.h"
22 #include <stdlib.h>
23 #include <math.h>
24 
25 #include "../Common/Global.h"
26 #include "../Utils/UtilsInterface.h"
27 #include "../Utils/Utils.h"
28 #include "../Utils/Constants.h"
29 #include "../Common/Windows.h"
30 
31 typedef enum
32 {
33   SKY_WATER = 0,
34   SKY_CHEKER,
35   ROOM,
36   BALCK,
37   WHITE
38 } TypeOfBackground;
39 static gchar* types[] = {N_("sky & water"), N_("sky & cheker"), N_("room"),  N_("black"), N_("white") };
40 static gint typeOfBackground[] = {SKY_WATER, SKY_CHEKER, ROOM, BALCK, WHITE };
41 static gint backgroundType = BALCK;
42 static gint tmpBackgoundType = BALCK;
43 
44 /**************************************************************************************************************************************/
applyPovrayOptions(GtkWidget * win,gpointer data)45 void applyPovrayOptions(GtkWidget *win, gpointer data)
46 {
47 	backgroundType = tmpBackgoundType;
48 }
49 /**************************************************************************************************************************************/
activateRadioButton(GtkWidget * button,gpointer data)50 static void activateRadioButton(GtkWidget *button, gpointer data)
51 {
52 	gint* type = NULL;
53 
54 	if(!GTK_IS_WIDGET(button)) return;
55 
56 	type = g_object_get_data(G_OBJECT (button), "Type");
57 	tmpBackgoundType = *type;
58 }
59 /**************************************************************************************************************************************/
addRadioButtonToATable(GtkWidget * table,GtkWidget * friendButton,gchar * label,gint i,gint j,gint k)60 static GtkWidget* addRadioButtonToATable(GtkWidget* table, GtkWidget* friendButton, gchar* label, gint i, gint j, gint k)
61 {
62 	GtkWidget *newButton;
63 
64 	if(friendButton)
65 		newButton = gtk_radio_button_new_with_label( gtk_radio_button_get_group (GTK_RADIO_BUTTON (friendButton)), label);
66 	else
67 		newButton = gtk_radio_button_new_with_label( NULL, label);
68 
69 	gtk_table_attach(GTK_TABLE(table),newButton,j,j+k,i,i+1,
70 		(GtkAttachOptions)	(GTK_FILL | GTK_EXPAND),
71 		(GtkAttachOptions)	(GTK_FILL | GTK_EXPAND),
72                   3,3);
73 
74 	g_object_set_data(G_OBJECT (newButton), "Type",NULL);
75 	return newButton;
76 }
77 /**************************************************************************************************************************************/
createPOVBackgroundFrame(GtkWidget * box)78 void createPOVBackgroundFrame(GtkWidget *box)
79 {
80 	GtkWidget* button;
81 	GtkWidget* frame;
82 	GtkWidget* vboxFrame;
83 	GtkWidget *table = gtk_table_new(5,2,TRUE);
84 	gint i;
85 
86 	frame = gtk_frame_new (_("Type of background"));
87 	gtk_widget_show (frame);
88 	gtk_box_pack_start (GTK_BOX (box), frame, TRUE, TRUE, 3);
89 	gtk_frame_set_label_align (GTK_FRAME (frame), 0.5, 0.5);
90 
91 	vboxFrame = gtk_vbox_new (FALSE, 3);
92 	gtk_widget_show (vboxFrame);
93 	gtk_container_add (GTK_CONTAINER (frame), vboxFrame);
94 
95 	gtk_box_pack_start (GTK_BOX (vboxFrame), table, TRUE, TRUE, 0);
96 
97 	button = NULL;
98 	for(i=0;i<=WHITE;i++)
99 	{
100 		button = addRadioButtonToATable(table, button, types[i], i, 0,2);
101 		g_object_set_data(G_OBJECT (button), "Type",&typeOfBackground[i]);
102 		g_signal_connect(G_OBJECT(button),"clicked", G_CALLBACK(activateRadioButton),NULL);
103 		if(backgroundType== typeOfBackground[i]) gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
104 	}
105 	tmpBackgoundType = backgroundType;
106 }
107 /****************************************************************************************************/
createPovrayOptionsWindow(GtkWidget * win)108 void createPovrayOptionsWindow(GtkWidget* win)
109 {
110 	GtkWidget *dialogWindow = NULL;
111 	GtkWidget *button;
112 	GtkWidget *frame;
113 	GtkWidget *hbox;
114 	gchar title[BSIZE];
115 
116 	dialogWindow = gtk_dialog_new();
117 	gtk_widget_realize(GTK_WIDGET(dialogWindow));
118 	sprintf(title, _("Povray options"));
119 	gtk_window_set_title(GTK_WINDOW(dialogWindow),title);
120 
121 	gtk_window_set_modal (GTK_WINDOW (dialogWindow), TRUE);
122 	gtk_window_set_position(GTK_WINDOW(dialogWindow),GTK_WIN_POS_CENTER);
123 
124 	g_signal_connect(G_OBJECT(dialogWindow), "delete_event", (GCallback)destroy_button_windows, NULL);
125 	g_signal_connect(G_OBJECT(dialogWindow), "delete_event", (GCallback)gtk_widget_destroy, NULL);
126 
127 	frame = gtk_frame_new (NULL);
128 	gtk_widget_show (frame);
129 	gtk_box_pack_start (GTK_BOX (GTK_WIDGET(GTK_DIALOG(dialogWindow)->vbox)), frame, TRUE, TRUE, 3);
130 
131 	hbox = gtk_hbox_new (FALSE, 3);
132 	gtk_widget_show (hbox);
133 	gtk_container_add (GTK_CONTAINER (frame), hbox);
134 
135 	createPOVBackgroundFrame(hbox);
136 	gtk_box_set_homogeneous (GTK_BOX( GTK_DIALOG(dialogWindow)->action_area), TRUE);
137 
138 	button = create_button(dialogWindow,"Cancel");
139 	gtk_box_pack_end (GTK_BOX( GTK_DIALOG(dialogWindow)->action_area), button, FALSE, TRUE, 5);
140 	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
141 	g_signal_connect_swapped(G_OBJECT(button), "clicked", (GCallback)destroy_button_windows, GTK_OBJECT(dialogWindow));
142 	g_signal_connect_swapped(G_OBJECT(button), "clicked", (GCallback)gtk_widget_destroy, GTK_OBJECT(dialogWindow));
143 
144 	button = create_button(dialogWindow,"OK");
145 	gtk_box_pack_start (GTK_BOX( GTK_DIALOG(dialogWindow)->action_area), button, FALSE, TRUE, 5);
146 	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
147 	gtk_widget_grab_default(button);
148 	g_signal_connect_swapped(G_OBJECT(button), "clicked", (GCallback)applyPovrayOptions, GTK_OBJECT(dialogWindow));
149 	g_signal_connect_swapped(G_OBJECT(button), "clicked", (GCallback)destroy_button_windows, GTK_OBJECT(dialogWindow));
150 	g_signal_connect_swapped(G_OBJECT(button), "clicked", (GCallback)gtk_widget_destroy, GTK_OBJECT(dialogWindow));
151 
152 
153 	add_button_windows(title,dialogWindow);
154 
155 	gtk_widget_show_all(dialogWindow);
156 	if(GTK_IS_WIDGET(win)) gtk_window_set_transient_for(GTK_WINDOW(dialogWindow),GTK_WINDOW(win));
157 }
158 /********************************************************************************/
get_pov_sky()159 static gchar *get_pov_sky()
160 {
161      gchar *temp;
162      temp = g_strdup(
163 		"//  Sky with white clouds  \n"
164 		"object \n"
165 		"{\n"
166 		"\tsphere { <0, 0, 0> 200000}\n"
167 		"\ttexture {Blue_Sky scale <50000, 6000, 50000>}\n"
168 		"\tfinish { ambient 0.9 diffuse 0.0 }\n"
169 		"}\n"
170 		);
171 	 return temp;
172 }
173 /********************************************************************************/
get_pov_water(gdouble x,gdouble y,gdouble z,gdouble scale)174 static gchar *get_pov_water(gdouble x, gdouble y, gdouble z, gdouble scale)
175 {
176      gchar *temp;
177      temp = g_strdup_printf(
178 		"//  water\n"
179 		"plane \n"
180 		"{\n"
181 		"\t<%f, %f, %f>,%f\n"
182 		"\ttexture\n"
183 		"\t{\n"
184 		"\t\tpigment{ rgb <0.2, 0.2, 0.2> }\n"
185 		"\t\tnormal { bumps 0.08 scale <1,0.25,0.35>*1 turbulence 0.6 }\n"
186 		"\t\tfinish { ambient 0.05 diffuse 0.55 brilliance 6.0 phong 0.8 phong_size 120 reflection 0.6 }\n"
187 		"\t}\n"
188 		"}\n",
189 		x, y, z, scale
190 		);
191 	 return temp;
192 }
193 /********************************************************************************/
get_pov_checker(gdouble x,gdouble y,gdouble z,gdouble scale)194 static gchar *get_pov_checker(gdouble x, gdouble y, gdouble z, gdouble scale)
195 {
196      gchar *temp;
197      temp = g_strdup_printf(
198 		"// checker\n"
199 		"plane \n"
200 		"{\n"
201 		"\t<%f, %f, %f>,%f\n"
202 		"\thollow on\n"
203 		"\tpigment { checker rgb <1,0,0>, rgb <1,1,0> scale 10 }\n"
204 		"\tfinish { ambient 0.3 diffuse 0.4 }\n"
205 		"}\n",
206 		x, y, z, scale
207 		);
208 	 return temp;
209 }
210 /********************************************************************************/
get_pov_hexagon(gdouble x,gdouble y,gdouble z,gdouble scale)211 static gchar *get_pov_hexagon(gdouble x, gdouble y, gdouble z, gdouble scale)
212 {
213      gchar *temp;
214      temp = g_strdup_printf(
215 		"// Hexagon\n"
216 		"plane \n"
217 		"{\n"
218 		"\t<%f, %f, %f>,%f\n"
219 		"\tpigment { hexagon rgb <0.5,0.5,0.5>, rgb <0,0,0>, rgb <1,1,1> scale 5}\n"
220 		"\tfinish { ambient 0.3 diffuse 0.4 }\n"
221 		"}\n",
222 		x, y, z, scale
223 		);
224 	 return temp;
225 }
226 /********************************************************************************/
get_pov_stone(gdouble x,gdouble y,gdouble z,gdouble scale,gint numStone)227 static gchar *get_pov_stone(gdouble x, gdouble y, gdouble z, gdouble scale, gint numStone)
228 {
229      gchar *temp;
230      temp = g_strdup_printf(
231 		"//  Stone\n"
232 		"plane \n"
233 		"{\n"
234 		"\t<%f, %f, %f>,%f\n"
235 		"\ttexture {T_Stone%d}\n"
236 		"\tfinish { ambient 0.5 diffuse 0.4 }\n"
237 		"}\n",
238 		x, y, z, scale, numStone
239 		);
240 	 return temp;
241 }
242 /********************************************************************************/
get_pov_backgrond_unicolor(gdouble red,gdouble green,gdouble blue)243 static gchar *get_pov_backgrond_unicolor(gdouble red, gdouble green, gdouble blue)
244 {
245      gchar *temp;
246      temp = g_strdup_printf(
247 		"// BACKGROUND \n"
248 		"background \n"
249 		"{\n"
250 		"\tcolor rgb < %f, %f, %f >\n"
251 		"}\n",
252 		red, green, blue
253 		);
254      return temp;
255 }
256 /********************************************************************************/
get_pov_sky_water(gdouble x,gdouble y,gdouble z,gdouble scale)257 static gchar *get_pov_sky_water(gdouble x, gdouble y, gdouble z, gdouble scale)
258 {
259 	gchar *temp = NULL;
260 	gchar *sky;
261 	gchar *water;
262 
263      	sky = get_pov_sky();
264 	water = get_pov_water(x, y, z, scale);
265 	if(sky)
266 	{
267 		temp = g_strdup(sky);
268 		g_free(sky);
269 	}
270 	if(water)
271 	{
272 		gchar* dump;
273 		dump =temp;
274 		temp = g_strdup_printf("%s%s",dump,water);
275 		g_free(dump);
276 		g_free(water);
277 	}
278 	return temp;
279 }
280 /********************************************************************************/
get_pov_sky_checker(gdouble x,gdouble y,gdouble z,gdouble scale)281 static gchar *get_pov_sky_checker(gdouble x, gdouble y, gdouble z, gdouble scale)
282 {
283 	gchar *temp = NULL;
284 	gchar *sky;
285 	gchar *checker;
286 
287      	sky = get_pov_sky();
288 	checker = get_pov_checker(x, y, z, scale);
289 	if(sky)
290 	{
291 		temp = g_strdup(sky);
292 		g_free(sky);
293 	}
294 	if(checker)
295 	{
296 		gchar* dump;
297 		dump =temp;
298 		temp = g_strdup_printf("%s%s",dump,checker);
299 		g_free(dump);
300 		g_free(checker);
301 	}
302 	return temp;
303 }
304 /********************************************************************************/
get_pov_cube(gdouble xScale,gdouble yScale,gdouble zScale)305 static gchar *get_pov_cube(gdouble xScale, gdouble yScale, gdouble zScale)
306 {
307 	gchar *temp = NULL;
308 	gchar *bottom;
309 	gchar *left;
310 	gchar *ground;
311 
312 	bottom = get_pov_hexagon(0, 1, 0, yScale);
313 	if(bottom)
314 	{
315 		temp = g_strdup(bottom);
316 		g_free(bottom);
317 	}
318 	left = get_pov_stone(1, 0, 0, xScale*2,21);
319 	if(left)
320 	{
321 		gchar* dump;
322 		dump =temp;
323 		temp = g_strdup_printf("%s%s",dump,left);
324 		g_free(dump);
325 		g_free(left);
326 	}
327 	ground = get_pov_stone(0, 0, 1, zScale*8,21);
328 	if(ground)
329 	{
330 		gchar* dump;
331 		dump =temp;
332 		temp = g_strdup_printf("%s%s",dump,ground);
333 		g_free(dump);
334 		g_free(ground);
335 	}
336 	return temp;
337 }
338 /********************************************************************************/
get_pov_background(gdouble xScale,gdouble yScale,gdouble zScale)339 gchar *get_pov_background(gdouble xScale, gdouble yScale, gdouble zScale)
340 {
341 	switch(backgroundType)
342 	{
343 		case SKY_WATER : return get_pov_sky_water(0,1,0, yScale);
344 		case SKY_CHEKER : return get_pov_sky_checker(0, 1, 0, yScale);
345 		case ROOM : return get_pov_cube(xScale, yScale, zScale);
346 		case  BALCK : return get_pov_backgrond_unicolor(0,0,0);
347 		case  WHITE :return get_pov_backgrond_unicolor(1,1,1);
348 		default: return NULL;
349 	}
350 }
351 /********************************************************************************/
352