1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2022 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "simil-measure-base.h" |
---|
6 | #include <frams/vm/classes/genoobj.h> |
---|
7 | #include <frams/model/geometry/meshbuilder.h> |
---|
8 | #include <frams/model/geometry/geometryutils.h> |
---|
9 | |
---|
10 | SimilMeasureBase::SimilMeasureBase() |
---|
11 | { |
---|
12 | for (int i = 0; i < 2; i++) |
---|
13 | { |
---|
14 | genos[i] = nullptr; |
---|
15 | models[i] = nullptr; |
---|
16 | } |
---|
17 | } |
---|
18 | |
---|
19 | double SimilMeasureBase::evaluateDistance(const Geno* G0, const Geno* G1) |
---|
20 | { |
---|
21 | genos[0] = G0; |
---|
22 | genos[1] = G1; |
---|
23 | |
---|
24 | // create models of objects to compare |
---|
25 | models[0] = newModel(genos[0]); |
---|
26 | models[1] = newModel(genos[1]); |
---|
27 | |
---|
28 | if (models[0] == NULL) |
---|
29 | logPrintf("SimilMeasureBase", "evaluateDistance", LOG_ERROR, "Unable to create a Model from genotype '%s'.", G0->getGenes().c_str()); |
---|
30 | if (models[1] == NULL) |
---|
31 | logPrintf("SimilMeasureBase", "evaluateDistance", LOG_ERROR, "Unable to create a Model from genotype '%s'.", G1->getGenes().c_str()); |
---|
32 | if (models[0] == NULL || models[1] == NULL) |
---|
33 | return -1; |
---|
34 | |
---|
35 | double distance = getDistance(); |
---|
36 | SAFEDELETE(models[0]); |
---|
37 | SAFEDELETE(models[1]); |
---|
38 | return distance; |
---|
39 | } |
---|
40 | |
---|
41 | void SimilMeasureBase::p_evaldistance(ExtValue * args, ExtValue * ret) |
---|
42 | { |
---|
43 | Geno * g1 = GenoObj::fromObject(args[1]); |
---|
44 | Geno * g2 = GenoObj::fromObject(args[0]); |
---|
45 | if ((!g1) || (!g2)) |
---|
46 | ret->setEmpty(); |
---|
47 | else |
---|
48 | ret->setDouble(evaluateDistance(g1, g2)); |
---|
49 | } |
---|
50 | |
---|
51 | Model* SimilMeasureBase::newModel(const Geno *g) |
---|
52 | { |
---|
53 | if (g == NULL) |
---|
54 | { |
---|
55 | logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "NULL genotype pointer"); |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | Model *m = new Model(*g, ModelEnum::SHAPETYPE_UNKNOWN); |
---|
59 | if (!m->isValid()) |
---|
60 | { |
---|
61 | logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "Invalid model for the genotype of '%s'", g->getName().c_str()); |
---|
62 | delete m; |
---|
63 | return NULL; |
---|
64 | } |
---|
65 | return m; |
---|
66 | } |
---|
67 | |
---|
68 | Model SimilMeasureBase::sampleSurface(Model* M, double density) |
---|
69 | { |
---|
70 | Model resultModel; |
---|
71 | resultModel.open(); |
---|
72 | |
---|
73 | MeshBuilder::ModelSurface iterator(density); |
---|
74 | iterator.initialize(M); |
---|
75 | |
---|
76 | Pt3D point; |
---|
77 | while (iterator.tryGetNext(point)) |
---|
78 | { |
---|
79 | GeometryUtils::addPointToModel(point, resultModel); |
---|
80 | } |
---|
81 | |
---|
82 | resultModel.close(); |
---|
83 | return resultModel; |
---|
84 | } |
---|