-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
563 lines (493 loc) · 19.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include <stdio.h>
#include <stdlib.h>
#include <aws/lambda-runtime/runtime.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <aws/core/utils/memory/stl/SimpleStringStream.h>
#include <CATBody.h>
#include <CATSoftwareConfiguration.h>
#include <CATTopData.h>
#include <CATMathPoint.h>
#include <CATMathVector.h>
#include <CATMathAxis.h>
#include <CATMathPlane.h>
#include <CATMathBox.h>
#include <CATGeoFactory.h>
#include <CATICGMTopOperator.h>
#include <CATErrorMacros.h>
#include <CATICGMSolidCuboid.h>
#include <CATICGMSolidCylinder.h>
#include <CATICGMTopRevol.h>
#include <CATICGMSolidSphere.h>
#include <CATICGMSolidTorus.h>
#include <CATICGMSolidPyramid.h>
#include <CATICGMCloseOperator.h>
#include <CATCGMContainerMngt.h>
#include <CATGeoFactoryMngt.h>
#include <CATCloneManager.h>
#include <CATSGMC1GeoUtilities.h>
#include <CATCGMStream.h>
#include <CATCGMNoMoreMemory.h>
#include <CATCGMTable.h>
#include <CATLib.h>
#include <CATMath.h>
#include <CATPoint.h>
#include <CATPlane.h> // Geometric plane
#include <CATVertex.h> // Vertex
#include <CATFace.h> // Face
#include <CATCone.h>
#include <ListPOfCATCell.h>
#include <ListPOfCATFace.h>
#include <ListPOfCATVertex.h>
#include <CATCrvLimits.h> // Limits on a curve
#include <CATPCurve.h>
#include <CATPLine.h>
#include <CATICGMTopSkin.h> // skin operator
#include <CATTopObject.h>
#include <CATTopology.h>
#include <PrimitiveData.h>
#include <fstream.h>
using namespace std;
using namespace aws::lambda_runtime;
#define TRACE_ERR(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
fflush(stderr); \
}
//#define _DEBUG
static void OrderPrimitveFaces(CATBody * piBody, CATMathPoint & origin, CATMathVector & axis, CATLISTP(CATFace) & orderedFaces)
{
TRACE_ERR("Running in OrderPrimitveFaces() \n")
// process face sequence, if exist, the first is the from face and the last is to face
// lateral faces should be ordered by the from face
//CATLISTP(CATFace) faces;
//piBody->GetAllCells((CATLISTP(CATCell)&) faces, 2);
CATLISTP(CATCell) faces;
piBody->GetAllCells(faces, 2);
CATFace * ptFromFace=NULL, * ptToFace=NULL;
double resolution=piBody->GetContainer()->GetResolution(CatC0);
for(int item=1;item<=faces.Size();++item)
{
CATFace* pFace = (CATFace*)faces[item];
CATPlane * ptPlane=(CATPlane *) pFace->GetSurface();
if(ptPlane->IsATypeOf(CATPlaneType))
{
CATMathPoint planeOrigin;
CATMathVector planeNormal;
ptPlane->GetNormal(planeOrigin, planeNormal);
if(planeNormal.IsParallel(axis))
{
CATMathVector dvec=(planeOrigin-origin);
dvec.Normalize();
if(fabs(dvec*planeNormal)<resolution)
{
//ptFromFace=faces[item];
ptFromFace=pFace;
orderedFaces.Append(ptFromFace);
}
else
{
//ptToFace=faces[item];
ptToFace=pFace;
}
faces.RemovePosition(item);
item--;
if(ptToFace && ptFromFace)
break;
}
}
}
// assume the lateral faces are ordered properly from construction sequence
//orderedFaces.Append(faces);
for(int item=1;item<=faces.Size();++item)
{
CATFace* pFace = (CATFace*)faces[item];
orderedFaces.Append(pFace);
}
if(ptToFace)
orderedFaces.Append(ptToFace);
}
static CATBody *CreatePrimitive(CATGeoFactory *pFactoryIn, CATSoftwareConfiguration *pCATConfigIn, const PrimitiveData *pData)
{
TRACE_ERR("Running in CreatePrimitive() \n")
CATTopData topData(pCATConfigIn);
CATMathPoint origin(pData->getX(), pData->getY(), pData->getZ());
double width = pData->getWidth();
double length = pData->getLength();
double height = pData->getHeight();
CATICGMTopOperator * pOperator=NULL;
CATBody* pPrimitive=NULL;
CATMathDirection axis(0.,0.,1.);
CATTry
{
PrimitiveType primitiveType = pData->getPrimitiveType();
switch(primitiveType)
{
case PrimitiveType::Cuboid: //cuboid: origin defines the center point of base face
case PrimitiveType::Pyramid: //pyramid: use cuboid base face
{
CATMathPoint iPt1(origin.GetX()-width/2.0, origin.GetY()-length/2.0, origin.GetZ());
CATMathPoint iPt2(iPt1.GetX()+width, iPt1.GetY(), iPt1.GetZ());
CATMathPoint iPt3(iPt1.GetX(), iPt1.GetY()+length, iPt1.GetZ());
CATMathPoint iPt4(iPt1.GetX(), iPt1.GetY(), iPt1.GetZ()+height);
CATICGMSolidCuboid * pCuboid=CATCGMCreateSolidCuboid(pFactoryIn, &topData, iPt1, iPt2, iPt3, iPt4);
pOperator=pCuboid;
pCuboid->Run();
pPrimitive=pCuboid->GetResult();
if(primitiveType == PrimitiveType::Pyramid) // use the base face to create final shape
{
CATLISTP(CATFace) orderedFaces;
OrderPrimitveFaces(pPrimitive, origin, axis, orderedFaces);
pOperator->Release();
CATICGMSolidPyramid * pPyramidOp=CATCGMCreateSolidPyramid(pFactoryIn, &topData, orderedFaces[1], origin+axis*height);
pOperator=pPyramidOp;
pPyramidOp->Run();
pFactoryIn->Remove(pPrimitive);
pPrimitive=pPyramidOp->GetResult();
}
}
break;
case PrimitiveType::Cylinder: // cylinder: width is diameter
{
CATMathPoint topCenter(origin.GetX(), origin.GetY(), origin.GetZ()+height);
double radius=width/2.0;
// Create operator to create cylinder
CATICGMSolidCylinder* pCylinderOp = CATCGMTopCreateSolidCylinder(pFactoryIn, &topData, origin, topCenter, radius);
if (pCylinderOp != NULL)
TRACE_ERR("\t Successfully getting pCylinderOp = %p \n", (void *)pCylinderOp)
else
TRACE_ERR("\t Failed getting pCylinderOp \n")
// Run the operator and return the result.
pOperator=pCylinderOp;
pCylinderOp->Run();
pPrimitive = pCylinderOp->GetResult();
}
break;
case PrimitiveType::Cone: // cone: width is diameter, height locates apex
{
CATMathPoint iBaseCenter = origin;
CATMathPoint iTopCenter(origin.GetX(), origin.GetY(), origin.GetZ()+height);
double iBottomRadius = width;
double iTopRadius = 0.;
double truncatedHeight = (iTopCenter-iBaseCenter).Norm();
//The truncated height is same as full cone height in the case when top radius is zero.
double fullConeHeight = truncatedHeight;
// Calculate the full cone height when top radius is greater than zero.
if(iTopRadius > 0)
{
fullConeHeight = truncatedHeight * iBottomRadius / (iBottomRadius - iTopRadius);
}
CATMathPoint apex = iTopCenter;
// Calculate the Apex of the full cone in case the full cone height is greater than the truncated height.
double ratio = fullConeHeight / truncatedHeight;
if(ratio > 1.0)
{
apex = iBaseCenter + (iTopCenter - iBaseCenter) * ratio;
}
// Computes cone parameters
CATMathVector coneNormal = apex - iBaseCenter;
CATMathDirection axisK(coneNormal);
CATMathDirection axisI(CATMathI);
if ( axisI.IsParallel(axisK) )
{
axisI = CATMathJ;
}
CATMathDirection axisJ(axisK ^ axisI);
axisI = axisK ^ axisJ;
CATMathAxis coneAxis(apex, axisI, axisJ, -axisK);
fullConeHeight = coneNormal.Norm();
double coneAngle = CATAtan(fullConeHeight/iBottomRadius);
double endRuledLength(fullConeHeight/sin(coneAngle));
double startRuledLength = endRuledLength * iTopRadius / iBottomRadius;
// Create a cone geometry
// If an exception is thrown in this workflow, the CGMFactoryRemovable_uptr object will remove this object from the container.
CATCone* pMyCone = pFactoryIn->CreateCone(coneAxis, 0.0, coneAngle, 0.0, CAT2PI, startRuledLength, endRuledLength);
// Ensure that the object will not be deleted by reference counting
// until it is explicitly removed from the container.
pMyCone->SetMode( CatCGMExplicit );
// Create a sheet cone
// Note: Recall that the GetLimits method may not always return the expected limits.
// We can use it here since we have just created the surface and there has been
// no chance for the limits to be reset by another operation.
CATSurLimits surLimits;
pMyCone->GetLimits(surLimits);
CATICGMTopSkin* pSkinOp = CATCGMCreateTopSkin(pFactoryIn, &topData, pMyCone, &surLimits);
pOperator=pSkinOp;
pSkinOp->Run();
CATBody *sheetCone = pSkinOp->GetResult();
pOperator->Release();
if (NULL != sheetCone)
{
CATICGMCloseOperator* pCloseOp = CATCGMCreateCloseOperator(pFactoryIn, &topData, sheetCone);
pOperator=pCloseOp;
pCloseOp->Run();
pPrimitive = pCloseOp->GetResult();
}
}
break;
case PrimitiveType::Sphere: // sphere: width is diameter
{
CATICGMSolidSphere * pSphereOp=CATCGMCreateSolidSphere(pFactoryIn, &topData, origin, width/2.0);
pOperator=pSphereOp;
pSphereOp->Run();
pPrimitive = pSphereOp->GetResult();
}
break;
case PrimitiveType::Torus: // torus: width is major radius, height is minor radius
{
CATMathAxis torusAxis(origin, CATMathVector(1,0,0), CATMathVector(0,1,0));
CATICGMSolidTorus * pTorusOp=CATCGMCreateSolidTorus(pFactoryIn, &topData, torusAxis, origin+CATMathPoint(width/2.0, 0, 0), origin+CATMathPoint(width/2.0, 0, height/2.0));
pOperator=pTorusOp;
pTorusOp->Run();
pPrimitive = pTorusOp->GetResult();
}
break;
default:
break;
}
}
CATCatch(CATError, error)
{
if (pPrimitive)
{
pPrimitive->GetContainer()->Remove(pPrimitive);
pPrimitive=NULL;
}
}
CATEndTry;
if (pOperator)
pOperator->Release();
return(pPrimitive);
}
static bool save(CATBody *pBodyIn, const char *pFileName)
{
TRACE_ERR("Running in save() \n")
CATGeoFactory *pContainer = NULL;
CATTry
{
pContainer = CATCreateCGMContainer();
TRACE_ERR("\t Called CATCreateCGMContainer() \n")
CATCloneManager cloneMan(pContainer, CatCGMFullDuplicate, NULL);
cloneMan.Add(pBodyIn);
cloneMan.Run();
TRACE_ERR("\t Body is cloned \n")
ofstream filetowrite(pFileName, ios::binary);
CATSaveCGMContainer((CATGeoFactory *)pContainer, filetowrite);
filetowrite.close();
if (!filetowrite)
TRACE_ERR("\t Failed writing the body into ofstream \n")
pContainer->Release();
}
CATCatch(CATError, err)
{
TRACE_ERR("\t Exception is thrown. The error message is: %s \n", err->GetMessageText())
// TRACE_ERR("\t Exception is thrown. The error message is: %s \n", err->GetNLSMessage().ConvertToChar())
if (pContainer)
pContainer->Release();
return false;
}
CATEndTry;
return true;
}
static invocation_response my_handler(invocation_request const &request)
{
TRACE_ERR("Running in my_handler() \n")
using namespace Aws::Utils::Json;
JsonValue json_request(request.payload);
if (!json_request.WasParseSuccessful())
{
return invocation_response::failure("Failed to parse input JSON", "InvalidJSON");
}
auto request_view = json_request.View();
if (request_view.ValueExists("primitive_type"))
{
TRACE_ERR("\t parsing payload data \n")
std::string typeStr = request_view.GetString("primitive_type");
PrimitiveType primitive_type = PrimitiveType::None;
if (typeStr == "primitive_cuboid")
primitive_type = PrimitiveType::Cuboid;
if (typeStr == "primitive_pyramid")
primitive_type = PrimitiveType::Pyramid;
else if (typeStr == "primitive_cylinder")
primitive_type = PrimitiveType::Cylinder;
else if (typeStr == "primitive_cone")
primitive_type = PrimitiveType::Cone;
else if (typeStr == "primitive_sphere")
primitive_type = PrimitiveType::Sphere;
else if (typeStr == "primitive_torus")
primitive_type = PrimitiveType::Torus;
else
{
TRACE_ERR("\t Wrong primitive_type parameter!!! exception thrown \n")
return invocation_response::failure("'primitive_type' is wrong", "InvalidJSON");
}
double x = request_view.GetDouble("location_X");
double y = request_view.GetDouble("location_Y");
double z = request_view.GetDouble("location_Z");
double l = request_view.GetDouble("size_length");
double w = request_view.GetDouble("size_width");
double h = request_view.GetDouble("size_height");
PrimitiveData data(primitive_type, x, y, z, l, w, h);
TRACE_ERR("\t To call CATCreateCGMContainer \n")
CATGeoFactory *pFactory = CATCreateCGMContainer();
if (pFactory != NULL)
TRACE_ERR("\t Successfully getting pFactory = %p \n", (void *)pFactory)
else
TRACE_ERR("\t Failed getting pFactory \n")
TRACE_ERR("\t To call CATCreateSoftwareConfiguration \n")
CATSoftwareConfiguration *pConfig = CATCreateSoftwareConfiguration();
if (pConfig != NULL)
TRACE_ERR("\t Successfully getting pConfig = %p \n", (void *)pConfig)
else
TRACE_ERR("\t Failed getting pConfig \n")
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
TRACE_ERR("\t Successfully getting pBody = %p \n", (void *)pBody)
else
TRACE_ERR("\t Failed getting pBody \n")
std::string pathname = "/mnt/ws/CGM_R425/" + typeStr + ".ncgm";
save(pBody, pathname.c_str());
CATCloseCGMContainer(pFactory);
pFactory = NULL;
JsonValue response;
response.WithString("lambda_prism2 return Status", "done");
return invocation_response::success(response.View().WriteCompact(), "application/json");
}
else
{
return invocation_response::failure("'primitive_type' not in payload", "InvalidJSON");
}
}
static void runTests()
{
TRACE_ERR("Running in runTests() \n")
TRACE_ERR("\t To call CATCreateCGMContainer \n")
CATGeoFactory *pFactory = CATCreateCGMContainer();
if (pFactory != NULL)
TRACE_ERR("\t Successfully getting pFactory = %p \n", (void *)pFactory)
else
TRACE_ERR("\t Failed getting pFactory \n")
TRACE_ERR("\t To call CATCreateSoftwareConfiguration \n")
CATSoftwareConfiguration *pConfig = CATCreateSoftwareConfiguration();
if (pConfig != NULL)
TRACE_ERR("\t Successfully getting pConfig = %p \n", (void *)pConfig)
else
TRACE_ERR("\t Failed getting pConfig \n")
{// test 1: Cuboid
PrimitiveData data(PrimitiveType::Cuboid, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting cuboid pBody = %p \n", (void *)pBody)
save(pBody, "cuboid.ncgm");
}
else
{
TRACE_ERR("\t Failed getting cuboid pBody \n")
}
}
{// test 2: Pyramid
PrimitiveData data(PrimitiveType::Pyramid, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting pyramid pBody = %p \n", (void *)pBody)
save(pBody, "pyramid.ncgm");
}
else
{
TRACE_ERR("\t Failed getting pyramid pBody \n")
}
}
{// test 3: Cylinder
PrimitiveData data(PrimitiveType::Cylinder, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting cylinder pBody = %p \n", (void *)pBody)
save(pBody, "cylinder.ncgm");
}
else
{
TRACE_ERR("\t Failed getting cylinder pBody \n")
}
}
{// test 4: Cone
PrimitiveData data(PrimitiveType::Cone, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting cone pBody = %p \n", (void *)pBody)
save(pBody, "cone.ncgm");
}
else
{
TRACE_ERR("\t Failed getting cone pBody \n")
}
}
{// test 5: Sphere
PrimitiveData data(PrimitiveType::Sphere, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting sphere pBody = %p \n", (void *)pBody)
save(pBody, "sphere.ncgm");
}
else
{
TRACE_ERR("\t Failed getting sphere pBody \n")
}
}
{// test 6: Torus
PrimitiveData data(PrimitiveType::Torus, 0, 0, 0, 100, 100, 40);
TRACE_ERR("\t To call CreatePrimitive \n")
CATBody *pBody = CreatePrimitive(pFactory, pConfig, &data);
if (pBody != NULL)
{
TRACE_ERR("\t Successfully getting torus pBody = %p \n", (void *)pBody)
save(pBody, "torus.ncgm");
}
else
{
TRACE_ERR("\t Failed getting torus pBody \n")
}
}
CATCloseCGMContainer(pFactory);
pFactory = NULL;
}
static void initializeCGM()
{
TRACE_ERR("Running in initializeCGM() \n")
int status = CATCGMTable(0, 314159);
printf("CATCGMTable returns status = %d\n", status);
#ifdef _DEBUG
CATPutEnv("CATDictionaryPath=/home/zhn/git/lambda-prism/ThirdParty/CGM_R425/linux_a64/code/dictionary");
#endif
const char *pDicPath = CATGetEnv("CATDictionaryPath");
TRACE_ERR("\t CATDictionaryPath is set at: %s \n", pDicPath);
TRACE_ERR("\t To call CATCreateCGMContainer \n")
CATGeoFactory *pFactory = CATCreateCGMContainer();
if (pFactory != NULL)
TRACE_ERR("\t Successfully getting pFactory = %p \n", (void *)pFactory)
else
TRACE_ERR("\t Failed getting pFactory \n")
CATCloseCGMContainer(pFactory);
}
int main()
{
TRACE_ERR("Running in main() \n")
initializeCGM();
#ifdef _DEBUG
TRACE_ERR("\t Calling runTests() \n")
runTests();
#else
TRACE_ERR("\t Calling my_handler() \n")
run_handler(my_handler);
#endif
return 0;
}