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

Last change on this file since 1050 was 1048, checked in by Maciej Komosinski, 3 years ago

SimilMeasure? -> SimilMeasureBase?; introduced a new class (SimilMeasure?) that allows scripts to access all similarity measures; a number of minor fixes and improvements

File size: 2.0 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-base.h"
6#include <frams/vm/classes/genoobj.h>
7#include <frams/model/geometry/meshbuilder.h>
8#include <frams/model/geometry/geometryutils.h>
9
10SimilMeasureBase::SimilMeasureBase()
11{
12        for (int i = 0; i < 2; i++)
13        {
14                genos[i] = nullptr;
15                models[i] = nullptr;
16        }
17}
18
19double 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 || models[1] == NULL)
29        {
30                logPrintf("SimilarityMeasure", "EvaluateDistance", LOG_ERROR, "Unable to create model from one of the genotypes.");
31                return -1;
32        }
33       
34        double distance = getDistance();
35        SAFEDELETE(models[0]);
36        SAFEDELETE(models[1]);
37        return distance;
38}
39
40void SimilMeasureBase::p_evaldistance(ExtValue * args, ExtValue * ret)
41{
42        Geno * g1 = GenoObj::fromObject(args[1]);
43        Geno * g2 = GenoObj::fromObject(args[0]);
44        if ((!g1) || (!g2))
45                ret->setEmpty();
46        else
47                ret->setDouble(evaluateDistance(g1, g2));
48}
49
50Model* SimilMeasureBase::newModel(const Geno *g)
51{
52        if (g == NULL)
53        {
54                logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "NULL genotype pointer");
55                return NULL;
56        }
57        Model *m = new Model(*g, ModelEnum::SHAPETYPE_UNKNOWN);
58        if (!m->isValid())
59        {
60                logPrintf("SimilarityMeasure", "newModel", LOG_ERROR, "Invalid model for the genotype of '%s'", g->getName().c_str());
61                delete m;
62                return NULL;
63        }
64        return m;
65}
66
67Model SimilMeasureBase::sampleSurface(Model* M, double density)
68{
69    Model resultModel;
70    resultModel.open();
71    GeometryUtils::addAnchorToModel(resultModel);
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}
Note: See TracBrowser for help on using the repository browser.