moveit2
The MoveIt Motion Planning Framework for ROS 2.
round_collada_numbers.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 /*********************************************************************
5  * Software License Agreement (BSD License)
6  *
7  * Copyright (c) 2013, University of Colorado, Boulder
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the Univ of CO, Boulder nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *********************************************************************/
38 """
39 # Author: Dave Coleman
40 # Desc: Rounds all the numbers to <decimal places> places
41 
42 from __future__ import print_function
43 
44 from lxml import etree
45 import shlex
46 import sys
47 
48 
49 def doRound(values, decimal_places):
50  num_vector = shlex.split(values)
51  new_vector = []
52 
53  for num in num_vector:
54  new_num = round(float(num), decimal_places)
55  print("Old:", num, "New:", new_num)
56  new_vector.append(str(new_num))
57 
58  new = " ".join(new_vector)
59  # print('Original:', values, ' Updated: ', new)
60 
61  return new
62 
63 
64 # -----------------------------------------------------------------------------
65 
66 if __name__ == "__main__":
67 
68  # Check input arguments
69  try:
70  input_file = sys.argv[1]
71  output_file = sys.argv[2]
72  decimal_places = int(sys.argv[3])
73  assert len(sys.argv) < 5 # invalid num-arguments
74  except:
75  print(
76  "\nUsage: round_collada_numbers.py <input_dae> <output_dae> <decimal places>"
77  )
78  print("Rounds all the numbers to <decimal places> places\n")
79  sys.exit(-1)
80 
81  print("\nCollada Number Rounder")
82  print("Rounding numbers to", decimal_places, "decimal places\n")
83 
84  namespace = "http://www.collada.org/2008/03/COLLADASchema"
85  dom = etree.parse(input_file)
86 
87  # find elements of particular name
88  elements = dom.xpath("//ns:translate", namespaces={"ns": namespace})
89  for i in range(len(elements)):
90  elements[i].text = doRound(elements[i].text, decimal_places)
91 
92  # find elements of particular name
93  elements = dom.xpath("//ns:rotate", namespaces={"ns": namespace})
94  for i in range(len(elements)):
95  elements[i].text = doRound(elements[i].text, decimal_places)
96 
97  # find elements of particular name
98  elements = dom.xpath("//ns:min", namespaces={"ns": namespace})
99  for i in range(len(elements)):
100  elements[i].text = doRound(elements[i].text, decimal_places)
101 
102  # find elements of particular name
103  elements = dom.xpath("//ns:max", namespaces={"ns": namespace})
104  for i in range(len(elements)):
105  elements[i].text = doRound(elements[i].text, decimal_places)
106 
107  # find elements of particular name
108  elements = dom.xpath("//ns:float", namespaces={"ns": namespace})
109  for i in range(len(elements)):
110  elements[i].text = doRound(elements[i].text, decimal_places)
111 
112  # save changes
113  with open(output_file, "wb") as f:
114  dom.write(f, encoding="utf-8")
def doRound(values, decimal_places)
void print(PropagationDistanceField &pdf, int numX, int numY, int numZ)