source: trunk/src/testing/app/volvis/caDataManagerClient.cpp @ 4

Revision 4, 17.9 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * OptiStore - A Data Processing Server
3 * Copyright (C) 2003 Electronic Visualization Laboratory,
4 * University of Illinois at Chicago
5 *
6 * Authors: Luc Renambot luc@evl.uic.edu
7 *          Shalini Venkataraman shalini@evl.uic.edu
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 *  * Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *  * Redistributions in binary form must reproduce the above
17 *    copyright notice, this list of conditions and the following disclaimer
18 *    in the documentation and/or other materials provided with the distribution.
19 *  * Neither the name of the University of Illinois at Chicago nor
20 *    the names of its contributors may be used to endorse or promote
21 *    products derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Direct questions, comments etc about OptiStore to luc@evl.uic.edu
36 *****************************************************************************/
37
38#include "caDataManagerClient.h"
39
40int cDataManagerClient::RBUDP_PORT = 38049;
41
42//a simple lightweight function to compare a string with a wildcard
43//1 if there is a match, 0 otherwise
44int wildcmp(char *wild, char *string) {
45        char *newString, *newWild;
46        while ((*string) && (*wild != '*')) {
47                //if two char are normal characters they should match
48                if ((*wild != *string) && (*wild != '?'))
49                        return 0;
50                wild++;
51                string++;
52        }
53        //found a "*"
54        while (*string) {
55                if (*wild == '*') {
56                        //last char - found a match
57                        if(!*++wild)
58                                return 1;
59                        newWild = wild;
60                        newString = string+1;
61                }
62                //if we found a '?' just increment both by 1
63                else if ((*wild == *string) || (*wild == '?')) {
64                        wild++;
65                        string++;
66                }
67                //need to search with new string and wild card
68                else {
69                        wild = newWild;
70                        string = newString++;
71                }
72        }
73        //make sure that the remaining chars are just *
74        while (*wild == '*')
75                wild++;
76        return !*wild;
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/////    cDataManagetClient class  /////////////////////////////////////////////
81////////////////////////////////////////////////////////////////////////////////
82
83
84cDataManagerClient::cDataManagerClient(char *_hostname)
85{
86    hostname = strdup(_hostname);
87    client = new QUANTAnet_tcpClient_c;
88
89    char name[128];
90    client->getSelfIP(name);
91    fprintf(stderr, "Client> %s,%u\n", name, client->getSelfPort());
92
93    dataptr = NULL;
94    gradptr = NULL;
95    vb = NULL;
96    pb = NULL;
97
98                // Initialize lookup table for gradient directions
99        InitDirectionsTable();
100
101    RBUDP_PORT = RBUDP_PORT + 1;
102}
103
104void cDataManagerClient::Open(int _port)
105{
106    port = _port;
107
108        //client->setSockOptions(QUANTAnet_tcpClient_c::READ_BUFFER_SIZE, 2*256*256*256);
109
110    if (client->connectToServer(hostname, port) < 0)
111    {
112        fprintf(stderr, "Cannot connect to server [%s] port %d\n", hostname, port);
113        exit(-1);
114    }
115}
116
117void cDataManagerClient::Init()
118{
119    fprintf(stderr, "Sending init\n");
120
121        // Init phase
122    dataSize = sizeof(int);
123    command = Q_INIT;
124    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
125
126    if (status != QUANTAnet_tcpClient_c::OK)
127        client->showStatus(status, dataSize);
128
129    int val[2];
130    int _port;
131
132    fprintf(stderr, "Waiting for init reply\n");
133
134    dataSize = sizeof(int) * 2;
135        //status =  client->read((char*)&id, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
136    status =  client->read((char*)val, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
137        id = val[0];
138    _port = val[1];
139
140    fprintf(stderr, "Client> MyID is [%d]\n", id);
141
142#if defined(USE_RBUDP)
143    fprintf(stderr, "Client> My RBUDP port is [%d]\n", _port);
144
145    sleep(1);
146        // Get connection with the remote RBUDP server
147        //blastee = new QUANTAnet_rbudpReceiver_c(RBUDP_PORT);
148    blastee = new QUANTAnet_rbudpReceiver_c(_port);
149    blastee->init(hostname);
150#endif
151
152}
153
154void cDataManagerClient::Exit()
155{
156        // Exit phase
157    dataSize = sizeof(int);
158    command = Q_EXIT;
159    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
160
161    fprintf(stderr, "Client> Exit\n");
162}
163
164int cDataManagerClient::Query()
165{
166    int i;
167    char name[128];
168    int dims[3];
169
170        // Query phase
171    dataSize = sizeof(int);
172    command = Q_LIST;
173    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
174
175    if (status != QUANTAnet_tcpClient_c::OK)
176        client->showStatus(status, dataSize);
177
178    dataSize = sizeof(int);
179    status =  client->read((char*)&num_datasets, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
180
181    fprintf(stderr, "Client> There are [%d] datasets\n", num_datasets);
182    datasets = (DataDesc*)malloc(num_datasets*sizeof(DataDesc));
183    for (i=0;i<num_datasets;i++)
184    {
185        memset(name, 0, 128);
186        dataSize = 128;
187        status = client->read((char*)name,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
188        datasets[i].name = strdup(name);
189
190        dataSize = 3*sizeof(int);
191        status = client->read((char*)dims,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
192        datasets[i].x = dims[0];
193        datasets[i].y = dims[1];
194        datasets[i].z = dims[2];
195
196        //fprintf(stderr, "Client> \tDataset [%d] : [%s]  -- [%d %d %d]\n", i, name, dims[0], dims[1], dims[2]);
197    }
198
199    return num_datasets;
200}
201
202void cDataManagerClient::Load(int ds)
203{
204    int val;
205
206        // Load a dataset
207    dataSize = sizeof(int);
208    command = Q_LOAD;
209    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
210        // Dataset #
211    dataSize = sizeof(int);
212    val = ds;
213    status = client->write((char*)&val, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
214    fprintf(stderr, "Client> Picking dataset %d [%s]\n", val, datasets[val].name);
215}
216
217void cDataManagerClient::Resample(int sx, int sy, int sz)
218{
219        // Resample a dataset
220    dataSize = sizeof(int);
221    command = Q_SAMPLE;
222    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
223
224        // Parameter: 3 int for dimensions
225    dataSize = 3 * sizeof(int);
226    sizes[0] = sx;
227    sizes[1] = sy;
228    sizes[2] = sz;
229    status = client->write((char*)sizes, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
230    fprintf(stderr, "Client> Asking for resampling at [%3d %3d %3d]\n", sizes[0], sizes[1], sizes[2]);
231}
232
233
234void cDataManagerClient::Crop(int ox, int oy, int oz, int sx, int sy, int sz)
235{
236        // Crop a dataset
237    dataSize = sizeof(int);
238    command = Q_CROP;
239    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
240
241        // Parameter: 3 int for dimensions
242    dataSize = 6 * sizeof(int);
243    sizes[0] = ox;
244    sizes[1] = oy;
245    sizes[2] = oz;
246    sizes[3] = sx;
247    sizes[4] = sy;
248    sizes[5] = sz;
249    status = client->write((char*)sizes, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
250    fprintf(stderr, "Client> Asking for cropping from [%3d %3d %3d] to [%3d %3d %3d]\n",
251            sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
252
253
254        // Allocate data storage for volume
255    if (dataptr != NULL) free(dataptr);
256    dataptr = (unsigned char*)malloc ((sizes[3]-sizes[0]+1) *
257                                      (sizes[4]-sizes[1]+1) * (sizes[5]-sizes[2]+1) );
258
259    if (gradptr != NULL) free(gradptr);
260    gradptr = (unsigned short*)malloc ((sizes[3]-sizes[0]+1) *
261                                       (sizes[4]-sizes[1]+1) * (sizes[5]-sizes[2]+1)
262                                       * sizeof(unsigned short));
263
264        //client->setSockOptions(QUANTAnet_tcpClient_c::READ_BUFFER_SIZE, 2*sizes[0]*sizes[1]*sizes[2]);
265        //fflush(stdout);
266}
267
268
269unsigned char* cDataManagerClient::Volume()
270{
271        int amount;
272
273                /////////////////////////////////////////////////////////////////////////
274                // Volume
275                /////////////////////////////////////////////////////////////////////////
276        fprintf(stderr, "Client> Asking for volume\n");
277        dataSize = sizeof(int);
278        command = Q_VOL;
279        status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
280
281        dataSize = sizeof(int);
282        status = client->read((char*)&amount,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
283        dataSize = amount;
284#if defined(USE_RBUDP)
285        blastee->receive((void*)dataptr, dataSize, RBUDP_FRAME);
286#else
287        status = client->read((char*)dataptr,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
288#endif
289
290        fprintf(stderr, "Client> Got volume of %d bytes\n\n", amount);
291
292        if (status != QUANTAnet_tcpClient_c::OK)
293                client->showStatus(status, dataSize);
294                /////////////////////////////////////////////////////////////////////////
295                /////////////////////////////////////////////////////////////////////////
296
297        return dataptr;
298}
299
300unsigned short* cDataManagerClient::Gradient()
301{
302        int amount;
303
304                /////////////////////////////////////////////////////////////////////////
305                // Gradient
306                /////////////////////////////////////////////////////////////////////////
307        fprintf(stderr, "Client> Asking for gradient volume\n");
308        dataSize = sizeof(int);
309        command = Q_GRAD;
310        status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
311
312        dataSize = sizeof(int);
313        status = client->read((char*)&amount,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
314
315        dataSize = amount;
316#if defined(USE_RBUDP)
317        blastee->receive((void*)gradptr, dataSize, RBUDP_FRAME);
318#else
319        status = client->read((char*)gradptr,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
320#endif
321
322        fprintf(stderr, "Client> Got gradient volume of %d bytes\n\n", amount);
323
324        if (status != QUANTAnet_tcpClient_c::OK)
325                client->showStatus(status, dataSize);
326                /////////////////////////////////////////////////////////////////////////
327                /////////////////////////////////////////////////////////////////////////
328
329        return gradptr;
330}
331
332int* cDataManagerClient::Histogram()
333{
334                /////////////////////////////////////////////////////////////////////////
335                // Histogram command
336                /////////////////////////////////////////////////////////////////////////
337        fprintf(stderr, "Client> Asking for volume histogram\n");
338        dataSize = sizeof(int);
339        command = Q_HISTO;
340        status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
341
342        dataSize = 259*sizeof(int);
343    int *histo = (int*)malloc(dataSize);
344    memset(histo, 0, dataSize);
345        status = client->read((char*)histo,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
346
347        fprintf(stderr, "Client> Got volume histogram of %d bytes\n", dataSize);
348        fprintf(stderr, "Client>     Histogram: min %d max %d mean %d\n\n",
349            histo[0], histo[1], histo[2]);
350
351        if (status != QUANTAnet_tcpClient_c::OK)
352                client->showStatus(status, dataSize);
353                /////////////////////////////////////////////////////////////////////////
354                /////////////////////////////////////////////////////////////////////////
355
356        return histo;
357}
358
359unsigned char* cDataManagerClient::Histogram2D()
360{
361                /////////////////////////////////////////////////////////////////////////
362                // Histogram2D command
363                /////////////////////////////////////////////////////////////////////////
364        fprintf(stderr, "Client> Asking for volume gradient histogram2D\n");
365        dataSize = sizeof(int);
366        command = Q_HISTO2D;
367        status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
368
369        dataSize = 256*256*sizeof(unsigned char);
370    unsigned char *histo = (unsigned char*)malloc(dataSize);
371    memset(histo, 0, dataSize);
372        status = client->read((char*)histo,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
373
374        fprintf(stderr, "Client> Got gradient histogram2D of %d bytes\n", dataSize);
375
376        if (status != QUANTAnet_tcpClient_c::OK)
377                client->showStatus(status, dataSize);
378                /////////////////////////////////////////////////////////////////////////
379                /////////////////////////////////////////////////////////////////////////
380
381        return histo;
382}
383
384NetPointBuffer*
385cDataManagerClient::IsoPoints(int isoval, int inc, int maxp)
386{
387    int val[3];
388
389        /////////////////////////////////////////////////////////////////////////
390        // Isosurface
391        /////////////////////////////////////////////////////////////////////////
392    dataSize = sizeof(int);
393    command = Q_POINT;
394    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
395        // Parameter: iso value
396    dataSize = 3*sizeof(int);
397    val[0] = isoval;
398    val[1] = inc;
399    val[2] = maxp;
400    status = client->write((char*)&val[0], &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
401    fprintf(stderr, "Client> Asking for isopoints at [%d] increment [%d]\n", val[0], val[1]);
402
403    if (status != QUANTAnet_tcpClient_c::OK)
404        client->showStatus(status, dataSize);
405
406    if (pb)
407    {
408        free(pb->vertex);
409        free(pb);
410    }
411
412    pb = (NetPointBuffer*)malloc(sizeof(NetPointBuffer));
413    dataSize = 1 * sizeof(int);
414    status =  client->read((char*)sizes, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
415
416    pb->nbpts = sizes[0];
417    pb->vertex = (float*)malloc( pb->nbpts * 3 * sizeof(float) );
418
419    dataSize = pb->nbpts * 3 * sizeof(float);
420    status = client->read((char*)pb->vertex,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
421
422    fprintf(stderr, "Client> Got isopoints of %d bytes\n\n", dataSize);
423
424    if (status != QUANTAnet_tcpClient_c::OK)
425        client->showStatus(status, dataSize);
426
427        /////////////////////////////////////////////////////////////////////////
428        /////////////////////////////////////////////////////////////////////////
429    return pb;
430}
431
432
433NetVertexBuffer* cDataManagerClient::Isosurface(int isoval, int gaussp, float decim)
434{
435    int val[2], total;
436        float fval;
437
438        /////////////////////////////////////////////////////////////////////////
439        // Isosurface
440        /////////////////////////////////////////////////////////////////////////
441    dataSize = sizeof(int);
442    command = Q_ISO;
443    status = client->write((char*)&command, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
444        // Parameter: iso value
445    dataSize = 2 * sizeof(int);
446    val[0] = isoval;
447    val[1] = gaussp;
448    status = client->write((char*)&val, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
449    fprintf(stderr, "Client> Asking for isosurface at [%d]\n", val[0]);
450
451    dataSize = sizeof(float);
452    fval = decim;
453    status = client->write((char*)&fval, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
454
455    if (status != QUANTAnet_tcpClient_c::OK)
456        client->showStatus(status, dataSize);
457
458    if (vb)
459    {
460        free(vb->vertex);
461        free(vb->normal);
462        free(vb->index);
463        free(vb->length);
464        free(vb);
465    }
466
467    vb = (NetVertexBuffer*)malloc(sizeof(NetVertexBuffer));
468    dataSize = 3 * sizeof(int);
469    status =  client->read((char*)sizes, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
470
471    vb->nbpts     = sizes[0];
472    vb->nbindices = sizes[1];
473    vb->lengths   = sizes[2];
474
475    vb->vertex = (float*)malloc( vb->nbpts * 3 * sizeof(float) );
476    vb->normal = (float*)malloc( vb->nbpts * 3 * sizeof(float) );
477    vb->index  = (unsigned int*)malloc( vb->nbindices * sizeof(unsigned int) );
478    vb->length = (unsigned short*)malloc( vb->lengths * sizeof(unsigned short) );
479
480    dataSize = vb->nbpts * 3 * sizeof(float);
481    status = client->read((char*)vb->vertex,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
482    total = dataSize;
483    dataSize = vb->nbpts * 3 * sizeof(float);
484    status = client->read((char*)vb->normal,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
485    total += dataSize;
486    dataSize = vb->nbindices * sizeof(unsigned int);
487    status = client->read((char*)vb->index,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
488    total += dataSize;
489    dataSize = vb->lengths * sizeof(unsigned short);
490    status = client->read((char*)vb->length,&dataSize, QUANTAnet_tcpClient_c::BLOCKING);
491    total += dataSize;
492
493    fprintf(stderr, "Client> Got isosurface of %d bytes\n\n", total);
494
495    if (status != QUANTAnet_tcpClient_c::OK)
496        client->showStatus(status, dataSize);
497
498        /////////////////////////////////////////////////////////////////////////
499        /////////////////////////////////////////////////////////////////////////
500    return vb;
501}
502
503
504char *cDataManagerClient::Name(int _idx)
505{
506    if (_idx >=0 && _idx < num_datasets)
507        return datasets[_idx].name;
508    else
509        return NULL;
510}
511
512cDataManagerClient::~cDataManagerClient()
513{
514#if defined(USE_RBUDP)
515    blastee->close();
516#endif
517}
518
519//Given the symbolic name of a volume, returns its index
520//if the volume is not found return -1;
521int cDataManagerClient::Lookup(const char* name)
522{
523        for (int i=0;i<num_datasets;i++)
524    {
525                if (strcmp(name,datasets[i].name) == 0)
526                        return i;
527        }
528        return -1;
529}
530
531//Given the symbolic name of a volume as a wild card, returns no of
532//that correspond to this name,
533//if not found, return 0;
534//caller has to free the memory
535int cDataManagerClient::GetMatching(char* wildcard, int *&indexList)
536{
537        int curIndex = 0;
538        indexList = (int*)malloc(num_datasets*sizeof(int));
539        for (int i=0;i<num_datasets;i++)
540        {
541                if (wildcmp(wildcard,datasets[i].name))
542                        indexList[curIndex++] = i;
543        }
544        indexList = (int*)realloc(indexList,curIndex*sizeof(int));
545        return curIndex;
546}
547
548    //get the size of the datasets
549void cDataManagerClient::GetDataDims(unsigned int idx, unsigned int& x, unsigned int& y, unsigned int& z)
550{
551        x = datasets[idx].x;
552        y = datasets[idx].y;
553        z = datasets[idx].z;
554}
Note: See TracBrowser for help on using the repository browser.