diff --git a/common.go b/common.go index 811e789..cefc5c3 100644 --- a/common.go +++ b/common.go @@ -11,7 +11,7 @@ import ( "time" ) -const version = "0.98.366-experimental1" +const version = "0.98.367-experimental1" const appDisplayName = "Projection Mapper - Built by Instinct" diff --git a/dual_stereo_depth.go b/dual_stereo_depth.go index ec71ccc..febdadc 100644 --- a/dual_stereo_depth.go +++ b/dual_stereo_depth.go @@ -51,6 +51,7 @@ type DualStereoRig struct { // DualRoom3DResult holds the reconstructed 3D room depth map, metric depth, // surface normals, wall plane, and protruding object bump proposals. type DualRoom3DResult struct { + RigCaptureSHA367 string // Exact rig used by reconstruction, not a later installation pose. Rig *DualStereoRig Depth *depthMap // Normalized [0..1] depth in Left camera frame (0 = near, 1 = far) MetricZMeters []float32 // Metric/baseline-scaled Z depth in meters (Left camera frame) @@ -816,6 +817,7 @@ func ReconstructRoom3DDepth( if rig == nil || leftImg.W <= 0 || leftImg.H <= 0 || rightImg.W <= 0 || rightImg.H <= 0 { return nil, errors.New("missing calibrated dual rig or stereo images") } + captureRigSHA367:=metricRigDigest367(rig) // Reconstruct on a clean working grid (up to 320w) for fast, crisp dense stereo + bilateral filtering. workW := mini(320, leftImg.W) workH := maxi(16, int(math.Round(float64(workW)*float64(leftImg.H)/float64(leftImg.W)))) @@ -1118,6 +1120,7 @@ func ReconstructRoom3DDepth( } return &DualRoom3DResult{ + RigCaptureSHA367:captureRigSHA367, Rig: rig, Depth: dm, MetricZMeters: zFiltered, @@ -1495,6 +1498,11 @@ func saveDualDepthCandidate355(p *Project, res *DualRoom3DResult, sourcePhoto im authSHA := depthAuthoringSHA(p) metricBytes,metricErr:=encodeMetricRoom355(res.MetricZMeters,res.Depth.Valid,res.Depth.W,res.Depth.H,res.Rig.ScaleSource,depthSHA(stagedBytes),authSHA) if metricErr!=nil{return metricErr} + rigPath367:=filepath.Join(outDir,"dual-trinocular-rig.json") + rigBytes367,rigErr367:=depthReadBounded(rigPath367);if rigErr367!=nil{return rigErr367} + var metric367 metricRoomPayload355;if metricErr=json.Unmarshal(metricBytes,&metric367);metricErr!=nil{return metricErr} + if res.RigCaptureSHA367!=""&&res.RigCaptureSHA367==metricRigDigest367(res.Rig){metric367.RigSHA367=depthSHA(rigBytes367)} + metricBytes,metricErr=json.Marshal(metric367);if metricErr!=nil{return metricErr} metricPath:=filepath.Join(outDir,"dual-3d-camera-z355.json") if metricErr=writeFileAtomic(metricPath,metricBytes);metricErr!=nil{return metricErr} p.DepthMetric.MetricZFile=metricPath @@ -1502,6 +1510,8 @@ func saveDualDepthCandidate355(p *Project, res *DualRoom3DResult, sourcePhoto im p.DepthMetric.SourceSHA256=depthSHA(stagedBytes) p.DepthMetric.AuthoringSHA256=authSHA p.DepthMetric.ScaleSource=res.Rig.ScaleSource + p.DepthMetric.RigFile367=rigPath367 + p.DepthMetric.RigSHA367=metric367.RigSHA367 p.Depth = &DepthRef{ AuthoringSHA256: authSHA, Alignment: "dual-stereo-left-camera", diff --git a/metric_registration367.go b/metric_registration367.go new file mode 100644 index 0000000..c44a84d --- /dev/null +++ b/metric_registration367.go @@ -0,0 +1,99 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "math" +) + +// The camera-Z payload and projector pose must share a captured stereo frame. +// This does not certify physical accuracy, freshness of the physical room, or +// qualify any effect. It only rejects unsupported or changed registration. +func metricRigQualified367(d *DualStereoRig, p *T3Rig) bool { + if d == nil || p == nil || d.RigLeftProj == nil || d.ScaleSource != "given-distance" || p.ScaleSrc != "stereo-metric-locked" || p.Quality() != "MEASURED" || p.Flat { + return false + } + if d.LeftW != p.CamW || d.LeftH != p.CamH || d.ProjW != p.ProjW || d.ProjH != p.ProjH || d.LeftW < 1 || d.LeftH < 1 || d.ProjW < 1 || d.ProjH < 1 { + return false + } + if !t3Finite(d.BaselineM) || d.BaselineM <= 0 || !t3Finite(d.StereoRMS) || d.StereoRMS < 0 || d.StereoRMS > t3OKRMS || d.StereoInlier < 12 { + return false + } + if !metricRotation367(d.RLR) || !metricRotation367(p.R) || !d.TLR.finite() || !p.T.finite() { + return false + } + if math.Abs(d.TLR.len()-d.BaselineM) > math.Max(1e-6, d.BaselineM*.001) || p.T.len() <= 1e-6 || !t3Finite(p.Baseline) || math.Abs(p.T.len()-p.Baseline) > math.Max(1e-6, p.Baseline*.001) { + return false + } + for _, k := range []t3Intr{d.CamLeft, d.CamRight, p.Cam, p.Proj} { + if !t3Finite(k.F) || k.F <= 0 || !t3Finite(k.Cx) || !t3Finite(k.Cy) { + return false + } + } + if d.CamLeft != p.Cam || !t3Finite(p.RMSProj) || p.RMSProj < 0 || p.RMSProj > t3OKRMS { + return false + } + for _, v := range []float64{p.K1, p.K2, p.KAspect} { + if !t3Finite(v) { + return false + } + } + a, e := json.Marshal(d.RigLeftProj) + if e != nil { + return false + } + b, e := json.Marshal(p) + return e == nil && bytes.Equal(a, b) +} +func metricRotation367(r t3Mat) bool { + for _, v := range r { + if !t3Finite(v) { + return false + } + } + if math.Abs(r.det()-1) > .001 { + return false + } + for i := 0; i < 3; i++ { + for j := 0; j < 3; j++ { + want := 0. + if i == j { + want = 1 + } + if math.Abs(r.col(i).dot(r.col(j))-want) > .001 { + return false + } + } + } + return true +} +func (r *Renderer) boundMetricRig367() error { + if r == nil || r.P == nil || r.P.DepthMetric == nil { + return errors.New("no bound metric rig") + } + m := r.P.DepthMetric + if m.RigFile367 == "" || len(m.RigSHA367) != 64 { + return errors.New("metric projector pose has no capture-byte receipt; capture and save depth again") + } + raw, e := depthReadBounded(m.RigFile367) + if e != nil || depthSHA(raw) != m.RigSHA367 { + return errors.New("captured stereo/projector rig bytes changed") + } + var rig DualStereoRig + if json.Unmarshal(raw, &rig) != nil || !metricRigQualified367(&rig, r.P.T3Rig) { + return errors.New("camera-Z and projector pose do not have matching measured stereo registration") + } + return nil +} + +func metricRigDigest367(r *DualStereoRig) string { + if r == nil { + return "" + } + b, e := json.Marshal(r) + if e != nil { + return "" + } + return depthSHA(b) +} diff --git a/metric_registration367_test.go b/metric_registration367_test.go new file mode 100644 index 0000000..dcbefb2 --- /dev/null +++ b/metric_registration367_test.go @@ -0,0 +1,186 @@ +package main + +import ( + "encoding/json" + "image" + "os" + "path/filepath" + "testing" +) + +func metricFixture367(t *testing.T) (*Project, []*plan, *DualStereoRig) { + t.Helper() + p := roomTestProject(16, 16) + poly := [][2]float64{{0, 0}, {1, 0}, {1, 1}, {0, 1}} + p.Surfaces = []Surface{{ID: "wall", Cls: "wall", Poly: poly, Quad: poly}} + rp := &T3Rig{CamW: 16, CamH: 16, ProjW: 32, ProjH: 20, Cam: t3Intr{F: 20, Cx: 8, Cy: 8}, Proj: t3Intr{F: 40, Cx: 16, Cy: 10}, R: t3Eye, T: t3Vec{.3, 0, 0}, Baseline: .3, CamSource: "measured", ScaleSrc: "stereo-metric-locked", RMSProj: .3} + rig := &DualStereoRig{LeftW: 16, LeftH: 16, RightW: 16, RightH: 16, ProjW: 32, ProjH: 20, CamLeft: rp.Cam, CamRight: rp.Cam, RLR: t3Eye, TLR: t3Vec{.2, 0, 0}, BaselineM: .2, ScaleSource: "given-distance", StereoRMS: .3, StereoInlier: 100, RigLeftProj: rp} + dm := &depthMap{W: 16, H: 16, V: make([]float32, 256), Valid: make([]bool, 256)} + z := make([]float32, 256) + for i := range z { + dm.V[i] = .4 + dm.Valid[i] = true + z[i] = 2 + } + if e := SaveDualDepthToProject(p, &DualRoom3DResult{RigCaptureSHA367: metricRigDigest367(rig), Rig: rig, Depth: dm, MetricZMeters: z}, image.NewGray(image.Rect(0, 0, 16, 16)), t.TempDir(), false); e != nil { + t.Fatal(e) + } + plans := []*plan{{s: &p.Surfaces[0], photoScene: true, depthAuthoring: p.Depth.AuthoringSHA256}} + return p, plans, rig +} +func Test367MetricRegistrationReceiptAndTamper(t *testing.T) { + p, plans, _ := metricFixture367(t) + r := &Renderer{P: p} + if _, e := r.measuredMetricRoom355(plans); e != nil { + t.Fatal("valid registration refused", e) + } + raw, e := os.ReadFile(p.DepthMetric.RigFile367) + if e != nil { + t.Fatal(e) + } + if e = os.WriteFile(p.DepthMetric.RigFile367, append(raw, ' '), 0600); e != nil { + t.Fatal(e) + } + if _, e = r.measuredMetricRoom355(plans); e == nil { + t.Fatal("changed rig bytes accepted") + } + os.WriteFile(p.DepthMetric.RigFile367, raw, 0600) + p.DepthMetric.RigSHA367 = "" + if _, e = r.measuredMetricRoom355(plans); e == nil { + t.Fatal("legacy unbound pose accepted") + } +} +func Test367MetricRegistrationRejectsMismatchedScaleAndPose(t *testing.T) { + _, _, original := metricFixture367(t) + cases := []func(*DualStereoRig){func(d *DualStereoRig) { d.ScaleSource = "assumed-stereo-baseline" }, func(d *DualStereoRig) { d.RigLeftProj.ScaleSrc = "given-distance" }, func(d *DualStereoRig) { d.TLR[0] = .4 }, func(d *DualStereoRig) { d.RigLeftProj.R[0] = 2 }, func(d *DualStereoRig) { d.RigLeftProj.Proj.F = 0 }, func(d *DualStereoRig) { d.LeftW = 20 }, func(d *DualStereoRig) { d.StereoInlier = 0 }, func(d *DualStereoRig) { d.RigLeftProj.CamSource = "self-calibrated-stereo" }} + for i, change := range cases { + raw, _ := json.Marshal(original) + var d DualStereoRig + json.Unmarshal(raw, &d) + change(&d) + if metricRigQualified367(&d, d.RigLeftProj) { + t.Fatal("unsupported rig accepted", i) + } + } + raw, _ := json.Marshal(original) + var d DualStereoRig + json.Unmarshal(raw, &d) + other := *d.RigLeftProj + other.T[0] += .01 + if metricRigQualified367(&d, &other) { + t.Fatal("other projector pose accepted") + } +} +func Test367SaveBindsActualRigBytes(t *testing.T) { + p, _, _ := metricFixture367(t) + raw, e := os.ReadFile(p.DepthMetric.RigFile367) + if e != nil { + t.Fatal(e) + } + if depthSHA(raw) != p.DepthMetric.RigSHA367 || filepath.Base(p.DepthMetric.RigFile367) != "dual-trinocular-rig.json" { + t.Fatal("saved receipt not exact bytes") + } + if p.DepthMetric.MetricZFile == p.DepthMetric.RigFile367 { + t.Fatal("rig confused with metric payload") + } +} + +func Test367ReplacedInstallRigCannotBlessCapturedDepth(t *testing.T) { + _, _, rig := metricFixture367(t) + captureSHA := metricRigDigest367(rig) + changed := *rig + pose := *rig.RigLeftProj + pose.T[0] = .4 + pose.Baseline = .4 + changed.RigLeftProj = &pose + dm := &depthMap{W: 1, H: 1, V: []float32{.4}, Valid: []bool{true}} + p := roomTestProject(16, 16) + if e := SaveDualDepthToProject(p, &DualRoom3DResult{RigCaptureSHA367: captureSHA, Rig: &changed, Depth: dm, MetricZMeters: []float32{2}}, image.NewGray(image.Rect(0, 0, 16, 16)), t.TempDir(), false); e != nil { + t.Fatal(e) + } + if p.DepthMetric.RigSHA367 != "" { + t.Fatal("later replacement rig blessed as capture pose") + } + if e := (&Renderer{P: p}).boundMetricRig367(); e == nil { + t.Fatal("unbound replacement accepted") + } +} +func Test367PayloadCannotRebindOtherRigFile(t *testing.T) { + p, plans, rig := metricFixture367(t) + changed := *rig + changed.CreatedAt = "different capture" + raw, e := json.Marshal(changed) + if e != nil { + t.Fatal(e) + } + if e = os.WriteFile(p.DepthMetric.RigFile367, raw, 0600); e != nil { + t.Fatal(e) + } + p.DepthMetric.RigSHA367 = depthSHA(raw) + if _, e = (&Renderer{P: p}).measuredMetricRoom355(plans); e == nil { + t.Fatal("sidecar-only receipt rebind accepted") + } +} + +func Test367ActualReconstructionEmitsCaptureRigLineage(t *testing.T) { + la, ll, ra, rl, lf, rf, src := synthRoom3DScene(true) + k := t3Intr{F: 285, Cx: 160, Cy: 90} + rig, dl, dr, shared, e := SolveDualTrinocularRig(la, ll, ra, rl, lf, rf, 640, 360, &k, &k, .28) + if e != nil { + t.Fatal(e) + } + room, e := ReconstructRoom3DDepth(rig, ll, rl, dl, dr, shared) + if e != nil { + t.Fatal(e) + } + if room.RigCaptureSHA367 == "" || room.RigCaptureSHA367 != metricRigDigest367(rig) { + t.Fatal("reconstruction lineage missing") + } + p := roomTestProject(320, 180) + if e = SaveDualDepthToProject(p, room, src, t.TempDir(), false); e != nil { + t.Fatal(e) + } + if p.DepthMetric.RigSHA367 == "" { + t.Fatal("actual reconstruction receipt missing") + } + t.Logf("synthetic registration qualification=%v stereoRMS=%.3f allowed=%.3f", metricRigQualified367(rig, p.T3Rig), rig.StereoRMS, t3OKRMS) + raw, e := os.ReadFile(p.DepthMetric.MetricZFile) + if e != nil { + t.Fatal(e) + } + payload, e := decodeMetricRoom355(raw) + if e != nil || payload.RigSHA367 != p.DepthMetric.RigSHA367 { + t.Fatal("payload lineage differs", e) + } +} + +func Test367MissingCaptureLineageStaysUnqualified(t *testing.T) { + _, _, rig := metricFixture367(t) + p := roomTestProject(16, 16) + dm := &depthMap{W: 1, H: 1, V: []float32{.5}, Valid: []bool{true}} + if e := SaveDualDepthToProject(p, &DualRoom3DResult{Rig: rig, Depth: dm, MetricZMeters: []float32{2}}, image.NewGray(image.Rect(0, 0, 16, 16)), t.TempDir(), false); e != nil { + t.Fatal(e) + } + if p.DepthMetric.RigSHA367 != "" { + t.Fatal("missing capture lineage manufactured") + } + if e := (&Renderer{P: p}).boundMetricRig367(); e == nil { + t.Fatal("legacy capture admitted") + } +} +func Test367FailedRigWritePreservesProject(t *testing.T) { + p, _, rig := metricFixture367(t) + before, _ := json.Marshal(p) + dir := t.TempDir() + if e := os.Mkdir(filepath.Join(dir, "dual-trinocular-rig.json"), 0700); e != nil { + t.Fatal(e) + } + dm := &depthMap{W: 1, H: 1, V: []float32{.5}, Valid: []bool{true}} + if e := SaveDualDepthToProject(p, &DualRoom3DResult{Rig: rig, RigCaptureSHA367: metricRigDigest367(rig), Depth: dm, MetricZMeters: []float32{2}}, image.NewGray(image.Rect(0, 0, 16, 16)), dir, false); e == nil { + t.Fatal("rig write unexpectedly succeeded") + } + after, _ := json.Marshal(p) + if string(before) != string(after) { + t.Fatal("failed rig write changed project") + } +} diff --git a/metric_room355.go b/metric_room355.go index 031abc2..e4774c6 100644 --- a/metric_room355.go +++ b/metric_room355.go @@ -11,6 +11,7 @@ import ( // payload preserves the reconstructed camera-Z values and their real scale // provenance, separately from the existing relative-depth lighting image. type metricRoomPayload355 struct { + RigSHA367 string `json:"rig_sha367,omitempty"` Schema string `json:"schema"` W, H int Units string `json:"units"` diff --git a/metric_room_binding355.go b/metric_room_binding355.go index e4f5564..3673c21 100644 --- a/metric_room_binding355.go +++ b/metric_room_binding355.go @@ -43,11 +43,12 @@ func (r *Renderer) measuredMetricRoom355(plans []*plan) (metricRoomPayload355, e if e != nil || depthSHA(raw) != m.MetricZSHA256 { return none, errors.New("metric payload bytes changed") } + if e:=r.boundMetricRig367();e!=nil{return none,e} payload, e := decodeMetricRoom355(raw) if e != nil { return none, e } - if payload.AuthoringSHA256 != auth || payload.SourceSHA256 != d.SourceSHA256 || payload.ScaleSource != m.ScaleSource || payload.W != m.IW || payload.H != m.IH { + if payload.RigSHA367 != m.RigSHA367 || payload.AuthoringSHA256 != auth || payload.SourceSHA256 != d.SourceSHA256 || payload.ScaleSource != m.ScaleSource || payload.W != m.IW || payload.H != m.IH { return none, errors.New("metric payload receipts disagree") } return payload, nil diff --git a/project.go b/project.go index f4a2e21..dad0f20 100644 --- a/project.go +++ b/project.go @@ -93,6 +93,8 @@ type ScanRef struct { } type DepthMetricRef struct { + RigFile367 string `json:"rig_file367,omitempty"` + RigSHA367 string `json:"rig_sha367,omitempty"` //355: exact camera-Z payload. File remains reserved for the legacy model PNG. MetricZFile string `json:"metric_z_file,omitempty"` MetricZSHA256 string `json:"metric_z_sha256,omitempty"`