1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 #include <dx/dx.h>
9 
10 #include <dxconfig.h>
11 
12 
13 static Error TraverseToScreen(Object, float);
14 static Error ScaleScreen(Object, Object, float);
15 static Error  GetFactor(int, Camera, float *);
16 static Camera MakeNewCamera(Camera, float);
17 
18 
m_ScaleScreen(Object * in,Object * out)19 Error m_ScaleScreen (Object *in, Object *out)
20 {
21   float factor;
22   int final_res;
23   Object incopy=NULL;
24   Camera new_camera;
25 
26   if (!in[0])
27     {
28       DXSetError(ERROR_BAD_PARAMETER, "object must be specified");
29       goto error;
30     }
31 
32   if ((DXGetObjectClass(in[0]) != CLASS_STRING) &&
33       (DXGetObjectClass(in[0]) != CLASS_ARRAY))
34   {
35      incopy = DXCopy(in[0], COPY_STRUCTURE);
36      if (!incopy)
37         goto error;
38   }
39   else
40   {
41      DXSetError(ERROR_MISSING_DATA,"object must be specified");
42      goto error;
43   }
44 
45   if (!incopy && !in[2])
46     {
47       out[0] = incopy;
48       out[1] = in[3];
49       return OK;
50     }
51 
52   if (in[1])
53    if (!DXExtractFloat(in[1], &factor))
54     {
55       DXSetError(ERROR_BAD_PARAMETER, "scale_factor must be a scalar value");
56       goto error;
57     }
58 
59   if (in[2])
60     if (!DXExtractInteger(in[2], &final_res))
61     {
62       DXSetError(ERROR_BAD_PARAMETER, "final_res must be an integer");
63       goto error;
64     }
65 
66   if (in[1] && in[2])
67     {
68       DXWarning("both scale_factor and final_res are specified. Only scale_factor will be used");
69     }
70 
71   if (in[3])
72     {
73       if ((DXGetObjectClass(in[3]) != CLASS_CAMERA))
74 	{
75 	  DXSetError(ERROR_BAD_PARAMETER,"current_camera must be a camera");
76 	  goto error;
77 	}
78     }
79 
80   if (!in[1] && in[2] && !in[3])
81     {
82       DXSetError(ERROR_DATA_INVALID,"if final_res is specified, current_camera must be specified");
83       goto error;
84     }
85 
86   /* figure out the appropriate scale factor */
87   if (!in[1]){
88      if (in[3]) {
89          if (!GetFactor(final_res, (Camera)in[3], &factor))
90             goto error;
91      }
92      else {
93        factor = 1;
94      }
95   }
96 
97   if (!TraverseToScreen(incopy, factor))
98     goto error;
99 
100   out[0] = incopy;
101 
102   /* make the output camera, if appropriate */
103   if (in[3])
104     {
105       new_camera = MakeNewCamera((Camera)in[3], factor);
106       if (!new_camera)
107       {
108         goto error;
109       }
110       out[1] = (Object)new_camera;
111     }
112   else
113     out[1] = NULL;
114 
115 
116   return OK;
117  error:
118   DXDelete((Object)incopy);
119   return ERROR;
120 }
121 
122 
TraverseToScreen(Object o,float factor)123 static Error TraverseToScreen(Object o, float factor)
124 {
125   /* should traverse down to view-port relative coordinates */
126 
127   Object subo;
128   int i, position;
129 
130   switch (DXGetObjectClass(o)) {
131   case (CLASS_GROUP):
132     for (i=0; (subo = DXGetEnumeratedMember((Group)o, i, NULL)); i++) {
133       if (!TraverseToScreen(subo, factor))
134 	goto error;
135     }
136     break;
137   case (CLASS_FIELD):
138     return OK;
139   case (CLASS_CLIPPED):
140     if (!DXGetClippedInfo((Clipped)o, &subo, NULL))
141       goto error;
142     if (!TraverseToScreen(subo, factor))
143       goto error;
144     break;
145   case (CLASS_XFORM):
146     if (!DXGetXformInfo((Xform)o, &subo, NULL))
147       goto error;
148     if (!TraverseToScreen(subo, factor))
149       goto error;
150     break;
151   case (CLASS_SCREEN):
152 
153     if (!DXGetScreenInfo((Screen)o, &subo, &position, NULL))
154       goto error;
155 
156     if ((position == SCREEN_VIEWPORT)||(position == SCREEN_WORLD))
157       {
158 	if (!ScaleScreen(o, subo, factor))
159 	  goto error;
160       }
161     else
162       if (!TraverseToScreen(subo, factor))
163 	goto error;
164     break;
165   default:
166     break;
167   }
168 
169   return OK;
170  error:
171   return ERROR;
172 
173 }
174 
ScaleScreen(Object parent,Object child,float factor)175 static Error ScaleScreen(Object parent, Object child, float factor)
176 {
177   Object new;
178 
179   /* now scale */
180   new = (Object)DXNewXform(child, DXScale(factor, factor, 1.0));
181 
182   if (!DXSetScreenObject((Screen)parent, new))
183     goto error;
184 
185   return OK;
186  error:
187   return ERROR;
188 }
189 
190 
GetFactor(int res,Camera cam,float * factor)191 static Error GetFactor(int res, Camera cam, float *factor)
192 {
193    int xres;
194 
195    /* extract the current x resolution of the camera */
196    if (!DXGetCameraResolution(cam, &xres, NULL))
197      goto error;
198 
199    if (xres == 0)
200    {
201       DXSetError(ERROR_DATA_INVALID,"x resolution of current_camera = 0");
202       goto error;
203    }
204 
205    *factor = (float)res/(float)xres;
206    return OK;
207 
208 error:
209    return ERROR;
210 
211 }
MakeNewCamera(Camera cam,float factor)212 static Camera MakeNewCamera(Camera cam, float factor)
213 {
214    int xres, newres;
215    Camera newcam;
216 
217    if (!DXGetCameraResolution(cam, &xres, NULL))
218      return NULL;
219 
220    newres = factor*xres;
221 
222    newcam = (Camera)DXCopy((Object)cam, COPY_STRUCTURE);
223    if (!newcam)
224       return NULL;
225 
226    if (!DXSetResolution(newcam, newres, 1))
227       return NULL;
228 
229    return newcam;
230 
231 }
232