File: polygon2shape.m

package info (click to toggle)
octave-geometry 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: cpp: 5,358; python: 379; objc: 328; makefile: 25
file content (56 lines) | stat: -rw-r--r-- 1,947 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
50
51
52
53
54
55
56
## Copyright (C) 2019 Juan Pablo Carbajal
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.

## Author: Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
## Updated: 2019-05-14

## -*- texinfo -*-
## @deftypefn {Function File} {@var{shape} = } polygon2shape (@var{polygon})
## Converts a polygon to a shape with edges defined by smooth polynomials.
##
## @var{polygon} is a N-by-2 matrix, each row representing a vertex.
## @var{shape} is a N-by-1 cell, where each element is a pair of polynomials
## compatible with polyval.
##
## In its current state, the shape is formed by polynomials of degree 1. Therefore
## the shape representation costs more memory except for colinear points in the
## polygon.
##
## @seealso{shape2polygon, simplifyPolygon, polyval}
## @end deftypefn

function shape = polygon2shape (polygon)

  # Filter colinear points
  polygon = simplifyPolygon_geometry (polygon);

  np = size(polygon,1);
  # polygonal shapes are memory inefficient!!
  # TODO filter the regions where edge angles are canging slowly and fit
  # polynomial of degree 3;
  pp = nan (2*np,2);

  # Transform edges into polynomials of degree 1;
  # pp = [(p1-p0) p0];
  pp(:,1) = diff(polygon([1:end 1],:)).'(:);
  pp(:,2) = polygon.'(:);

  shape = mat2cell(pp, 2*ones (1,np), 2);

endfunction

%!test
%! pp = [0 0; 1 0; 1 1; 0 1];
%! s = polygon2shape (pp);