/* * Copyright 2017 The Cartographer Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using CartographerSharp.Models.Transform; using CartographerSharp.Transform; namespace CartographerSharp.Sensor; /// /// The fixed frame pose data (like GPS, pose, etc.) will be used in the optimization. /// public struct FixedFramePoseData(long time, Rigid3d? pose = null) { public long Time { get; set; } = time; public Rigid3d? Pose { get; set; } = pose; } /// /// Operations on FixedFramePoseData. /// public static class FixedFramePoseDataOperations { /// /// Converts 'pose_data' to a proto::FixedFramePoseData. /// public static Models.Sensor.FixedFramePoseData ToProto(FixedFramePoseData poseData) { return new Models.Sensor.FixedFramePoseData( poseData.Time, poseData.Pose.HasValue ? (Rigid3dProto)poseData.Pose.Value : default ); } /// /// Converts 'proto' to an FixedFramePoseData. /// public static FixedFramePoseData FromProto(Models.Sensor.FixedFramePoseData proto) { // Check if pose is set (equivalent to proto.has_pose() in C++) // In C#, since Rigid3dProto is a struct, we check if rotation quaternion is normalized // (a valid quaternion should have norm close to 1, default would be all zeros) Rigid3d? pose = null; var rot = proto.Pose.Rotation; var quatNorm = Math.Sqrt(rot.W * rot.W + rot.X * rot.X + rot.Y * rot.Y + rot.Z * rot.Z); // If quaternion is normalized (or close to normalized), pose is set if (quatNorm > 0.1) // Threshold to distinguish from default (0,0,0,0) { pose = (Rigid3d)proto.Pose; } return new FixedFramePoseData( proto.Timestamp, pose ); } }