File: coordinates_one_query.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (49 lines) | stat: -rw-r--r-- 1,369 bytes parent folder | download | duplicates (3)
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
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Weights/discrete_harmonic_weights.h>

// Typedefs.
using Kernel  = CGAL::Simple_cartesian<double>;
using FT      = typename Kernel::FT;
using Point_2 = typename Kernel::Point_2;

int main() {

  // Create a polygon and a query point.
  const std::vector<Point_2> polygon =
    { Point_2(0, 0), Point_2(1, 0), Point_2(1, 1), Point_2(0, 1) };
  const Point_2 query(0.5, 0.5);

  // Allocate memory for weights and coordinates.
  std::vector<FT> weights;
  weights.reserve(polygon.size());
  std::vector<FT> coordinates;
  coordinates.reserve(polygon.size());

  // Compute barycentric weights.
  CGAL::Weights::discrete_harmonic_weights_2(polygon, query, std::back_inserter(weights));
  assert(weights.size() == polygon.size());

  std::cout << "2D weights: ";
  for (const FT weight : weights) {
    std::cout << weight << " ";
  }
  std::cout << std::endl;

  // Normalize weights in order to get barycentric coordinates.
  FT sum = 0.0;
  for (const FT weight : weights) {
    sum += weight;
  }
  assert(sum != 0.0);
  for (const FT weight : weights) {
    coordinates.push_back(weight / sum);
  }
  assert(coordinates.size() == weights.size());

  std::cout << "2D coordinates: ";
  for (const FT coordinate : coordinates) {
    std::cout << coordinate << " ";
  }
  std::cout << std::endl;
  return EXIT_SUCCESS;
}