TTK
Loading...
Searching...
No Matches
LDistance.h
Go to the documentation of this file.
1
12
13#pragma once
14
15// Standard.
16#include <cmath>
17#include <cstdlib>
18#include <string>
19
20// Base code.
21#include <Debug.h>
22#include <Geometry.h>
23
24namespace ttk {
25
26 class LDistance : virtual public Debug {
27 public:
28 LDistance();
29
30 template <class dataType>
31 int execute(const dataType *const inputData1,
32 const dataType *const inputData2,
33 dataType *const outputData,
34 const std::string &distanceType,
35 const SimplexId vertexNumber);
36
37 template <class dataType, typename PowerFunc>
38 int computeLn(const dataType *const input1,
39 const dataType *const input2,
40 dataType *const output,
41 const int n,
42 const SimplexId vertexNumber,
43 const PowerFunc &powerFunc);
44
45 template <class dataType>
46 int computeLinf(const dataType *const input1,
47 const dataType *const input2,
48 dataType *const output,
49 const SimplexId vertexNumber);
50
51 inline double getResult() {
52 return result;
53 }
54
55 inline void setPrintRes(const bool data) {
56 this->printRes = data;
57 }
58
59 template <typename type>
60 static type abs_diff(const type var1, const type var2) {
61 return (var1 > var2) ? var1 - var2 : var2 - var1;
62 }
63
64 protected:
65 double result{};
66 bool printRes{true};
67 };
68} // namespace ttk
69
70// template functions
71template <class dataType>
72int ttk::LDistance::execute(const dataType *const inputData1,
73 const dataType *const inputData2,
74 dataType *const outputData,
75 const std::string &distanceType,
76 const SimplexId vertexNumber) {
77
78 Timer t;
79 int status;
80
81// Check variables consistency
82#ifndef TTK_ENABLE_KAMIKAZE
83 if(inputData1 == nullptr || inputData2 == nullptr) {
84 return -1;
85 }
86#endif
87
88 if(distanceType == "inf") {
89 status = computeLinf(inputData1, inputData2, outputData, vertexNumber);
90 } else {
91 int const n = stoi(distanceType);
92 if(n < 1)
93 return -4;
94
95 TTK_POW_LAMBDA(status = computeLn, dataType, n, inputData1, inputData2,
96 outputData, n, vertexNumber);
97 }
98
99 if(this->printRes) {
100 this->printMsg(
101 "Data-set processed", 1.0, t.getElapsedTime(), this->threadNumber_);
102 }
103
104 return status;
105}
106
107template <class dataType, typename PowerFunc>
108int ttk::LDistance::computeLn(const dataType *const input1,
109 const dataType *const input2,
110 dataType *const output,
111 const int n,
112 const ttk::SimplexId vertexNumber,
113 const PowerFunc &powerFunc) {
114 dataType sum = 0;
115
116// Compute difference for each point.
117#ifdef TTK_ENABLE_OPENMP
118#pragma omp parallel for num_threads(threadNumber_) reduction(+ : sum)
119#endif
120 for(ttk::SimplexId i = 0; i < vertexNumber; ++i) {
121 const dataType diff = abs_diff<dataType>(input1[i], input2[i]);
122 const dataType power = powerFunc(diff);
123
124 // Careful: huge dataset + huge values
125 // may exceed double capacity.
126 sum += power;
127
128 // Store difference.
129 if(output)
130 output[i] = power;
131 }
132
133 sum = Geometry::pow(sum, 1.0 / (double)n);
134
135 // Affect result.
136 result = (double)sum;
137 if(this->printRes) {
138 this->printMsg("L" + std::to_string(n)
139 + "-distance: " + std::to_string(result));
140 }
141
142 return 0;
143}
144
145template <class dataType>
146int ttk::LDistance::computeLinf(const dataType *const input1,
147 const dataType *const input2,
148 dataType *const output,
149 const ttk::SimplexId vertexNumber) {
150 if(vertexNumber < 1)
151 return 0;
152
153 dataType maxValue = abs_diff<dataType>(input1[0], input2[0]);
154
155// Compute difference for each point.
156#ifdef TTK_ENABLE_OPENMP
157#pragma omp parallel for num_threads(threadNumber_) reduction(max : maxValue)
158#endif
159 for(ttk::SimplexId i = 1; i < vertexNumber; ++i) {
160 const dataType iter = abs_diff<dataType>(input1[i], input2[i]);
161 if(iter > maxValue)
162 maxValue = iter;
163
164 // Store absolute difference in output.
165 if(output)
166 output[i] = iter;
167 }
168
169 // Affect result.
170 result = (double)maxValue;
171 if(this->printRes) {
172 this->printMsg("Linf-distance: " + std::to_string(result));
173 }
174
175 return 0;
176}
#define TTK_POW_LAMBDA(CALLEXPR, TYPE, EXPN,...)
Optimized Power function with lambdas.
Definition Geometry.h:430
Minimalist debugging class.
Definition Debug.h:88
TTK lDistance processing package.
Definition LDistance.h:26
int computeLinf(const dataType *const input1, const dataType *const input2, dataType *const output, const SimplexId vertexNumber)
Definition LDistance.h:146
double getResult()
Definition LDistance.h:51
int computeLn(const dataType *const input1, const dataType *const input2, dataType *const output, const int n, const SimplexId vertexNumber, const PowerFunc &powerFunc)
Definition LDistance.h:108
int execute(const dataType *const inputData1, const dataType *const inputData2, dataType *const outputData, const std::string &distanceType, const SimplexId vertexNumber)
Definition LDistance.h:72
double result
Definition LDistance.h:65
static type abs_diff(const type var1, const type var2)
Definition LDistance.h:60
void setPrintRes(const bool data)
Definition LDistance.h:55
double getElapsedTime()
Definition Timer.h:15
T1 pow(const T1 val, const T2 n)
Definition Geometry.h:456
The Topology ToolKit.
int SimplexId
Identifier type for simplices of any dimension.
Definition DataTypes.h:22
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)