• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "ConfigManager.h"
17 
18 #include "json/json.h"
19 
20 #include <fstream>
21 #include <math.h>
22 #include <assert.h>
23 
24 
25 static const float kDegreesToRadians = M_PI / 180.0f;
26 
27 
normalizeToPlusMinus180degrees(float theta)28 static float normalizeToPlusMinus180degrees(float theta) {
29     const float wraps = floor((theta+180.0f) / 360.0f);
30     return theta - wraps*360.0f;
31 }
32 
33 
readChildNodeAsFloat(const char * groupName,const Json::Value & parentNode,const char * childName,float * value)34 static bool readChildNodeAsFloat(const char* groupName,
35                                  const Json::Value& parentNode,
36                                  const char* childName,
37                                  float* value) {
38     // Must have a place to put the value!
39     assert(value);
40 
41     Json::Value childNode = parentNode[childName];
42     if (!childNode.isNumeric()) {
43         printf("Missing or invalid field %s in record %s", childName, groupName);
44         return false;
45     }
46 
47     *value = childNode.asFloat();
48     return true;
49 }
50 
51 
initialize(const char * configFileName)52 bool ConfigManager::initialize(const char* configFileName)
53 {
54     bool complete = true;
55 
56     // Set up a stream to read in the input file
57     std::ifstream configStream(configFileName);
58 
59     // Parse the stream into JSON objects
60     Json::Reader reader;
61     Json::Value rootNode;
62     bool parseOk = reader.parse(configStream, rootNode, false /* don't need comments */);
63     if (!parseOk) {
64         printf("Failed to read configuration file %s\n", configFileName);
65         printf("%s\n", reader.getFormatedErrorMessages().c_str());
66         return false;
67     }
68 
69 
70     //
71     // Read car information
72     //
73     {
74         Json::Value car = rootNode["car"];
75         if (!car.isObject()) {
76             printf("Invalid configuration format -- we expect a car description\n");
77             return false;
78         }
79         complete &= readChildNodeAsFloat("car", car, "width",       &mCarWidth);
80         complete &= readChildNodeAsFloat("car", car, "wheelBase",   &mWheelBase);
81         complete &= readChildNodeAsFloat("car", car, "frontExtent", &mFrontExtent);
82         complete &= readChildNodeAsFloat("car", car, "rearExtent",  &mRearExtent);
83     }
84 
85 
86     //
87     // Read display layout information
88     //
89     {
90         Json::Value displayArray = rootNode["displays"];
91         if (!displayArray.isArray()) {
92             printf("Invalid configuration format -- we expect an array of displays\n");
93             return false;
94         }
95 
96         mDisplays.reserve(displayArray.size());
97         for (auto&& node : displayArray) {
98             DisplayInfo info;
99             info.port = node.get("displayPort", 0).asUInt();
100             info.function = node.get("function", "").asCString();
101             info.frontRangeInCarSpace = node.get("frontRange", -1).asFloat();
102             info.rearRangeInCarSpace = node.get("rearRange", -1).asFloat();
103 
104             mDisplays.emplace_back(info);
105         }
106     }
107 
108 
109     //
110     // Car top view texture properties for top down view
111     //
112     {
113         Json::Value graphicNode = rootNode["graphic"];
114         if (!graphicNode.isObject()) {
115             printf("Invalid configuration format -- we expect a graphic description\n");
116             return false;
117         }
118         complete &= readChildNodeAsFloat("graphic", graphicNode, "frontPixel", &mCarGraphicFrontPixel);
119         complete &= readChildNodeAsFloat("display", graphicNode, "rearPixel",  &mCarGraphicRearPixel);
120     }
121 
122 
123     //
124     // Read camera information
125     // NOTE:  Missing positions and angles are not reported, but instead default to zero
126     //
127     {
128         Json::Value cameraArray = rootNode["cameras"];
129         if (!cameraArray.isArray()) {
130             printf("Invalid configuration format -- we expect an array of cameras\n");
131             return false;
132         }
133 
134         mCameras.reserve(cameraArray.size());
135         for (auto&& node: cameraArray) {
136             // Get data from the configuration file
137             Json::Value nameNode = node.get("cameraId", "MISSING");
138             const char *cameraId = nameNode.asCString();
139 
140             Json::Value usageNode = node.get("function", "");
141             const char *function = usageNode.asCString();
142 
143             float yaw   = node.get("yaw", 0).asFloat();
144             float pitch = node.get("pitch", 0).asFloat();
145             float roll  = node.get("roll", 0).asFloat();
146             float hfov  = node.get("hfov", 0).asFloat();
147             float vfov  = node.get("vfov", 0).asFloat();
148             bool  hflip = node.get("hflip", false).asBool();
149             bool  vflip = node.get("vflip", false).asBool();
150 
151             // Wrap the direction angles to be in the 180deg to -180deg range
152             // Rotate 180 in yaw if necessary to flip the pitch into the +/-90degree range
153             pitch = normalizeToPlusMinus180degrees(pitch);
154             if (pitch > 90.0f) {
155                 yaw += 180.0f;
156                 pitch = 180.0f - pitch;
157             }
158             if (pitch < -90.0f) {
159                 yaw += 180.0f;
160                 pitch = -180.0f + pitch;
161             }
162             yaw = normalizeToPlusMinus180degrees(yaw);
163             roll = normalizeToPlusMinus180degrees(roll);
164 
165             // Range check the FOV values to ensure they are postive and less than 180degrees
166             if (hfov > 179.0f) {
167                 printf("Pathological horizontal field of view %f clamped to 179 degrees\n", hfov);
168                 hfov = 179.0f;
169             }
170             if (hfov < 1.0f) {
171                 printf("Pathological horizontal field of view %f clamped to 1 degree\n", hfov);
172                 hfov = 1.0f;
173             }
174             if (vfov > 179.0f) {
175                 printf("Pathological horizontal field of view %f clamped to 179 degrees\n", vfov);
176                 vfov = 179.0f;
177             }
178             if (vfov < 1.0f) {
179                 printf("Pathological horizontal field of view %f clamped to 1 degree\n", vfov);
180                 vfov = 1.0f;
181             }
182 
183             // Store the camera info (converting degrees to radians in the process)
184             CameraInfo info;
185             info.position[0] = node.get("x", 0).asFloat();
186             info.position[1] = node.get("y", 0).asFloat();
187             info.position[2] = node.get("z", 0).asFloat();
188             info.yaw         = yaw   * kDegreesToRadians;
189             info.pitch       = pitch * kDegreesToRadians;
190             info.roll        = roll  * kDegreesToRadians;
191             info.hfov        = hfov  * kDegreesToRadians;
192             info.vfov        = vfov  * kDegreesToRadians;
193             info.hflip       = hflip;
194             info.vflip       = vflip;
195             info.cameraId    = cameraId;
196             info.function    = function;
197 
198             mCameras.emplace_back(info);
199         }
200     }
201 
202     // If we got this far, we were successful as long as we found all our child fields
203     return complete;
204 }
205