source: cpp/frams/model/similarity/simil-measure.cpp @ 1045

Last change on this file since 1045 was 1045, checked in by oriona, 3 months ago

Part of GeometryTestUtils? functions moved to GeometryUtils?.

File size: 2.5 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "simil-measure.h"
6#include <frams/vm/classes/genoobj.h>
7#include <frams/model/geometry/geometryutils.h>
8#include <frams/model/geometry/meshbuilder.h>
9
10#define FIELDSTRUCT SimilMeasure
11
12static ParamEntry simil_measure_paramtab[] = {
13        { "Similarity", 1, 2, "SimilMeasure", "Evaluates morphological dissimilarity.", },
14        { "measure_type", 0, 0, "Type of similarity measure", "d 1 3 1", FIELD(measure_type), "", },
15        { "evaluateDistance", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "evaluate model dissimilarity", "p f(oGeno,oGeno)", PROCEDURE(p_evaldistance), "Calculates dissimilarity between two models created from Geno objects.", },
16        { 0, },
17};
18
19#undef FIELDSTRUCT
20
21SimilMeasure::SimilMeasure() : localpar(simil_measure_paramtab, this)
22{
23        for (int i = 0; i < 2; i++)
24        {
25                genos[i] = nullptr;
26                models[i] = nullptr;
27        }
28        localpar.setDefault();
29}
30
31double SimilMeasure::evaluateDistance(const Geno* G0, const Geno* G1)
32{
33        genos[0] = G0;
34        genos[1] = G1;
35
36        // create models of objects to compare
37        models[0] = newModel(genos[0]);
38        models[1] = newModel(genos[1]);
39
40        if (models[0] == NULL || models[1] == NULL)
41        {
42                logPrintf("SimilarityMeasure", "EvaluateDistance", LOG_ERROR, "Unable to create model from one of the genotypes.");
43                return -1;
44        }
45       
46        double distance = getDistance();
47        SAFEDELETE(models[0]);
48        SAFEDELETE(models[1]);
49        return distance;
50}
51
52Model* SimilMeasure::newModel(const Geno *g)
53{
54        if (g == NULL)
55        {
56                logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "NULL genotype pointer");
57                return NULL;
58        }
59        Model *m = new Model(*g, ModelEnum::SHAPETYPE_UNKNOWN);
60        if (!m->isValid())
61        {
62                logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "Invalid model for the genotype of '%s'", g->getName().c_str());
63                delete m;
64                return NULL;
65        }
66        return m;
67}
68
69void SimilMeasure::p_evaldistance(ExtValue *args, ExtValue *ret)
70{
71        Geno *g1 = GenoObj::fromObject(args[1]);
72        Geno *g2 = GenoObj::fromObject(args[0]);
73        if ((!g1) || (!g2))
74                ret->setEmpty();
75        else
76                ret->setDouble(evaluateDistance(g1, g2));
77}
78
79Model SimilMeasure::sampleSurface(Model* M, double density)
80{
81    Model resultModel;
82    resultModel.open();
83    GeometryUtils::addAnchorToModel(resultModel);
84
85    MeshBuilder::ModelSurface iterator(density);
86    iterator.initialize(M);
87
88    Pt3D point;
89    while (iterator.tryGetNext(point))
90    {
91        GeometryUtils::addPointToModel(point, resultModel);
92    }
93
94    resultModel.close();
95    return resultModel;
96}
Note: See TracBrowser for help on using the repository browser.