|
| 1 | +import { InfoCircleOutlined } from "@ant-design/icons"; |
| 2 | +import { Col, Form, type FormInstance, InputNumber, Row, Slider, Tooltip, Typography } from "antd"; |
| 3 | +import FormItem from "antd/es/form/FormItem"; |
| 4 | +import { |
| 5 | + AXIS_TO_TRANSFORM_INDEX, |
| 6 | + EXPECTED_TRANSFORMATION_LENGTH, |
| 7 | + IDENTITY_TRANSFORM, |
| 8 | + doAllLayersHaveTheSameRotation, |
| 9 | + fromCenterToOrigin, |
| 10 | + fromOriginToCenter, |
| 11 | + getRotationMatrixAroundAxis, |
| 12 | +} from "oxalis/model/accessors/dataset_layer_transformation_accessor"; |
| 13 | +import BoundingBox from "oxalis/model/bucket_data_handling/bounding_box"; |
| 14 | +import { useCallback, useEffect, useMemo } from "react"; |
| 15 | +import type { APIDataLayer } from "types/api_flow_types"; |
| 16 | +import { FormItemWithInfo } from "./helper_components"; |
| 17 | + |
| 18 | +const { Text } = Typography; |
| 19 | + |
| 20 | +type AxisRotationFormItemProps = { |
| 21 | + form: FormInstance | undefined; |
| 22 | + axis: "x" | "y" | "z"; |
| 23 | +}; |
| 24 | + |
| 25 | +function getDatasetBoundingBoxFromLayers(layers: APIDataLayer[]): BoundingBox | undefined { |
| 26 | + if (!layers || layers.length === 0) { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + let datasetBoundingBox = BoundingBox.fromBoundBoxObject(layers[0].boundingBox); |
| 30 | + for (let i = 1; i < layers.length; i++) { |
| 31 | + datasetBoundingBox = datasetBoundingBox.extend( |
| 32 | + BoundingBox.fromBoundBoxObject(layers[i].boundingBox), |
| 33 | + ); |
| 34 | + } |
| 35 | + return datasetBoundingBox; |
| 36 | +} |
| 37 | + |
| 38 | +export const AxisRotationFormItem: React.FC<AxisRotationFormItemProps> = ({ |
| 39 | + form, |
| 40 | + axis, |
| 41 | +}: AxisRotationFormItemProps) => { |
| 42 | + const dataLayers: APIDataLayer[] = Form.useWatch(["dataSource", "dataLayers"], form); |
| 43 | + const datasetBoundingBox = useMemo( |
| 44 | + () => getDatasetBoundingBoxFromLayers(dataLayers), |
| 45 | + [dataLayers], |
| 46 | + ); |
| 47 | + // Update the transformations in case the user changes the dataset bounding box. |
| 48 | + useEffect(() => { |
| 49 | + if ( |
| 50 | + datasetBoundingBox == null || |
| 51 | + dataLayers[0].coordinateTransformations?.length !== EXPECTED_TRANSFORMATION_LENGTH || |
| 52 | + !form |
| 53 | + ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const rotationValues = form.getFieldValue(["datasetRotation"]); |
| 57 | + const transformations = [ |
| 58 | + fromCenterToOrigin(datasetBoundingBox), |
| 59 | + getRotationMatrixAroundAxis("x", rotationValues["x"]), |
| 60 | + getRotationMatrixAroundAxis("y", rotationValues["y"]), |
| 61 | + getRotationMatrixAroundAxis("z", rotationValues["z"]), |
| 62 | + fromOriginToCenter(datasetBoundingBox), |
| 63 | + ]; |
| 64 | + const dataLayersWithUpdatedTransforms = dataLayers.map((layer) => { |
| 65 | + return { |
| 66 | + ...layer, |
| 67 | + coordinateTransformations: transformations, |
| 68 | + }; |
| 69 | + }); |
| 70 | + form.setFieldValue(["dataSource", "dataLayers"], dataLayersWithUpdatedTransforms); |
| 71 | + }, [datasetBoundingBox, dataLayers, form]); |
| 72 | + |
| 73 | + const setMatrixRotationsForAllLayer = useCallback( |
| 74 | + (rotationInDegrees: number): void => { |
| 75 | + if (!form) { |
| 76 | + return; |
| 77 | + } |
| 78 | + const dataLayers: APIDataLayer[] = form.getFieldValue(["dataSource", "dataLayers"]); |
| 79 | + const datasetBoundingBox = getDatasetBoundingBoxFromLayers(dataLayers); |
| 80 | + if (datasetBoundingBox == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const rotationInRadians = rotationInDegrees * (Math.PI / 180); |
| 85 | + const rotationMatrix = getRotationMatrixAroundAxis(axis, rotationInRadians); |
| 86 | + const dataLayersWithUpdatedTransforms: APIDataLayer[] = dataLayers.map((layer) => { |
| 87 | + let transformations = layer.coordinateTransformations; |
| 88 | + if (transformations == null || transformations.length !== EXPECTED_TRANSFORMATION_LENGTH) { |
| 89 | + transformations = [ |
| 90 | + fromCenterToOrigin(datasetBoundingBox), |
| 91 | + IDENTITY_TRANSFORM, |
| 92 | + IDENTITY_TRANSFORM, |
| 93 | + IDENTITY_TRANSFORM, |
| 94 | + fromOriginToCenter(datasetBoundingBox), |
| 95 | + ]; |
| 96 | + } |
| 97 | + transformations[AXIS_TO_TRANSFORM_INDEX[axis]] = rotationMatrix; |
| 98 | + return { |
| 99 | + ...layer, |
| 100 | + coordinateTransformations: transformations, |
| 101 | + }; |
| 102 | + }); |
| 103 | + form.setFieldValue(["dataSource", "dataLayers"], dataLayersWithUpdatedTransforms); |
| 104 | + }, |
| 105 | + [axis, form], |
| 106 | + ); |
| 107 | + return ( |
| 108 | + <Row gutter={24}> |
| 109 | + <Col span={16}> |
| 110 | + <FormItemWithInfo |
| 111 | + name={["datasetRotation", axis]} |
| 112 | + label={`${axis.toUpperCase()} Axis Rotation`} |
| 113 | + info={`Change the datasets rotation around the ${axis}-axis.`} |
| 114 | + colon={false} |
| 115 | + > |
| 116 | + <Slider min={0} max={270} step={90} onChange={setMatrixRotationsForAllLayer} /> |
| 117 | + </FormItemWithInfo> |
| 118 | + </Col> |
| 119 | + <Col span={8} style={{ marginRight: -12 }}> |
| 120 | + <FormItem |
| 121 | + name={["datasetRotation", axis]} |
| 122 | + colon={false} |
| 123 | + label=" " /* Whitespace label is needed for correct formatting*/ |
| 124 | + > |
| 125 | + <InputNumber |
| 126 | + min={0} |
| 127 | + max={270} |
| 128 | + step={90} |
| 129 | + precision={0} |
| 130 | + onChange={(value: number | null) => |
| 131 | + // InputNumber might be called with null, so we need to check for that. |
| 132 | + value != null && setMatrixRotationsForAllLayer(value) |
| 133 | + } |
| 134 | + /> |
| 135 | + </FormItem> |
| 136 | + </Col> |
| 137 | + </Row> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +type AxisRotationSettingForDatasetProps = { |
| 142 | + form: FormInstance | undefined; |
| 143 | +}; |
| 144 | + |
| 145 | +export type DatasetRotation = { |
| 146 | + x: number; |
| 147 | + y: number; |
| 148 | + z: number; |
| 149 | +}; |
| 150 | + |
| 151 | +export const AxisRotationSettingForDataset: React.FC<AxisRotationSettingForDatasetProps> = ({ |
| 152 | + form, |
| 153 | +}: AxisRotationSettingForDatasetProps) => { |
| 154 | + const dataLayers: APIDataLayer[] = form?.getFieldValue(["dataSource", "dataLayers"]); |
| 155 | + const isRotationOnly = useMemo(() => doAllLayersHaveTheSameRotation(dataLayers), [dataLayers]); |
| 156 | + |
| 157 | + if (!isRotationOnly) { |
| 158 | + return ( |
| 159 | + <Tooltip |
| 160 | + title={ |
| 161 | + <div> |
| 162 | + Each layers transformations must be equal and each layer needs exactly 5 affine |
| 163 | + transformation with the following schema: |
| 164 | + <ul> |
| 165 | + <li>Translation to the origin</li> |
| 166 | + <li>Rotation around the x-axis</li> |
| 167 | + <li>Rotation around the y-axis</li> |
| 168 | + <li>Rotation around the z-axis</li> |
| 169 | + <li>Translation back to the original position</li> |
| 170 | + </ul> |
| 171 | + To easily enable this setting, delete all coordinateTransformations of all layers in the |
| 172 | + advanced tab, save and reload the dataset settings. |
| 173 | + </div> |
| 174 | + } |
| 175 | + > |
| 176 | + <Text type="secondary"> |
| 177 | + Setting a dataset's rotation is only supported when all layers have the same rotation |
| 178 | + transformation. <InfoCircleOutlined /> |
| 179 | + </Text> |
| 180 | + </Tooltip> |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + return ( |
| 185 | + <div> |
| 186 | + <AxisRotationFormItem form={form} axis="x" /> |
| 187 | + <AxisRotationFormItem form={form} axis="y" /> |
| 188 | + <AxisRotationFormItem form={form} axis="z" /> |
| 189 | + </div> |
| 190 | + ); |
| 191 | +}; |
0 commit comments