drawCurve.cpp 1.36 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
// Copyright 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.

// Helper function to draw pen strokes with
// nPlanes = 3, feature vector = (1,dx,dy)
extern "C" void scn_2_drawCurve(void **m, THFloatTensor *features,
                                THFloatTensor *stroke) {
  auto location = THLongTensor_newWithSize1d(2);
  auto location_ = THLongTensor_data(location);

  auto vec = THFloatTensor_newWithSize1d(3);
  auto vec_ = THFloatTensor_data(vec);

  int n = THFloatTensor_size(stroke, 0) - 1;
  float *s = THFloatTensor_data(stroke); // stroke is a [n+1,2] array
  long idx = 0;
  float x1, y1, x2, y2; // n line segments (x1,y1) to (x2,y2)
  x2 = s[idx++];
  y2 = s[idx++];
  for (int i = 0; i < n; ++i) {
    x1 = x2;
    y1 = y2;
    x2 = s[idx++];
    y2 = s[idx++];
    float inverse_length =
        powf(1e-10 + (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), -0.5);
    vec_[0] = 1;
    vec_[1] = (x2 - x1) * inverse_length;
    vec_[2] = (y2 - y1) * inverse_length;
    for (float a = 0; a < 1; a += inverse_length) {
      location_[0] = x1 * a + x2 * (1 - a);
      location_[1] = y1 * a + y2 * (1 - a);
      scn_2_setInputSpatialLocation(m, features, location, vec, false);
    }
  }
  THLongTensor_free(location);
  THFloatTensor_free(vec);
}