How to establish a custom axis?

There is a 2 * 2 * 2 meter Housebox and coordinate axis X, Y, and Z, and I would like to know the formula for converting the custom axis to X, Y, and Z coordinates. Or methods.
I have a cube that can rotate. Three custom axes are set on the front and left sides. Assuming red represents axis 1, green represents axis 2, and blue represents axis 3:

A ray is projected forward of each axis (i.e. in the vertical direction of the plane), and the intersection point between the ray and the wall can be obtained. When the current rotation is 0, 0, 0, because axis 3 is vertical upwards and the ray is perpendicular to the ceiling, the coordinates are set to (axis 1, axis 2, 0), which coincide with the point hit by the ceiling of the room. If the coordinates are set to (axis 1, axis 2,500), then the object is at a height of 500 from the ceiling. The problem now is that this cube will rotate. If it rotates:

At this point, I set the axis to (axis 1, axis 2, 0), and it will look like this:

That is to say, how to set a point as the origin of a custom axis. How to convert axis 1, axis 2, and axis 3 to X, Y, and Z positions is World Location.

What formula is used for this. Actually, I am a handicraft enthusiast and want to make a homemade positioning hardware. A cube with a rotating center, three LiDARs on the front, left, and top. The known conditions are: the distance of three LiDARs (the ray distance from the radar to the wall) and the rotation of the hardware. I want to walk around the room with it. The size of the room is known, can this be used to create positioning hardware? What other conditions are needed?